-
Notifications
You must be signed in to change notification settings - Fork 4
/
simplivity-host-state.py
119 lines (89 loc) · 3.4 KB
/
simplivity-host-state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import sys
import os
import requests
import datetime
import optparse
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import json
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
######################################################################
# Parse Arguments
parser = optparse.OptionParser()
parser.add_option('-O', '--omnistack', help='IP of the OmniStack host', dest='arg_ominstack', type='string')
parser.add_option('-F', '--authfile', help='Json authfile user & password', dest='arg_authfile', type='string')
# parser.add_option('-U', '--username', help='OmniStack username (user@domain)', dest='arg_username', type='string')
# parser.add_option('-P', '--password', help='OmniStack password', dest='arg_password', type='string')
(opts, args) = parser.parse_args()
######################################################################
# Set the base URL for REST API requests.
global OVC_IP
OVC_IP = opts.arg_ominstack
url = 'https://'+OVC_IP+'/api/'
authfile=opts.arg_authfile
with open('./auth_simplivity.json') as json_data_file:
data = json.load(json_data_file)
username=data['simplivity']['user']
password=data['simplivity']['passwd']
# Set Nagios data
return_code = { 'OK': 0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN': 3 }
return_msg = ''
return_perfdata = ''
hosts_excluded = ''
######################################################################
# Functions
def connect_api():
try:
# Authenticate user and generate access token.
response = requests.post(url+'oauth/token', auth=('simplivity', ''), verify=False, data={
'grant_type':'password',
'username':username,
'password':password})
access_token = response.json()['access_token']
# Add the access_token to the header.
global headers
headers = {'Authorization': 'Bearer ' + access_token, 'Accept' : 'application/vnd.simplivity.v1+json'}
except requests.exceptions.ConnectionError:
print('Can not connect to host, please check IP address or hostname')
except KeyError:
print('Username or password error, please check and retry')
#--------------------------------------------------------------------
def output_nagios(return_msg, return_perfdata, return_code):
print return_msg
sys.exit(return_code)
#--------------------------------------------------------------------
def get_host():
try:
global Host
connect_api()
response = requests.get(url+'hosts', verify=False, headers=headers)
Hosts = dict()
Hosts = response.json()['hosts']
for Host in Hosts:
if Host['management_ip'] == OVC_IP:
return Host
except KeyError:
print('Failed to get host state')
def get_host_state():
try:
global return_msg
connect_api()
get_host()
if Host['state'] == 'ALIVE':
return_msg = 'The node: ' + Host['name'] + ' is in state: ' + Host['state']
output_nagios(return_msg,'',return_code['OK'])
elif Host['state'] == 'FAILED':
return_msg = 'Le noeud: ' + Host['name'] + ' is in state: ' + Host['state']
output_nagios(return_msg,'',return_code['CRITICAL'])
else:
return_msg = 'Le noeud: ' + Host['name'] + ' is in state: ' + Host['state']
output_nagios(return_msg,'',return_code['UNKNOWN'])
except KeyError:
print('Failed to get host state')
#--------------------------------------------------------------------
def main():
get_host_state()
# Start program
if __name__ == "__main__":
main()