-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfinibox_workload_graphite.py
221 lines (183 loc) · 5.74 KB
/
infinibox_workload_graphite.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'''
Examples:
Overall Performance Statistics:
python infinibox_workload_graphite.py
-u "http://<infinimetrics_fqdn>/api/rest/"
-s "<InfiniBox hostname>"
-C "systems/<serial#>/monitored_entities/1/data"
-f "format=json&page=last&page_size=1&sort=-timestamp"
-U "username"
-P "password"
Add -v to the end to view output
'''
from pprint import pformat
import requests
import time
import logging
import socket
import yaml
import functions
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# Disabled SSL certificate warnings for http api requests
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# define and gather command line options
parser = functions.ArgumentParserEx(
prog="check_infinidat",
description="Utilizes the Infinibox API to check status of monitored hardware and services.",
)
parser.add_argument(
"-u",
"--url",
dest="url",
default=None,
help="Specify the location of the Infinibox API. (default: %(default)s)"
)
parser.add_argument(
"-s",
"--servername",
dest="servername",
default=None,
help="Specify the short name of the Infinibox. (default: %(default)s)"
)
parser.add_argument(
"-U",
"--username",
dest="username",
default=None,
help="Username to use when connecting to API. (default: %(default)s)"
)
parser.add_argument(
"-P",
"--password",
dest="password",
default=None,
help="Password to use when connecting to API. (default: %(default)s)"
)
parser.add_argument(
"-C",
"--components",
dest="components",
default=None,
required=True,
help="Name of component to appent to url."
)
parser.add_argument(
"-f",
"--filters",
dest="filters",
default=None,
required=True,
help="Result filters to appent to url."
)
parser.add_argument(
"-t",
"--timeout",
dest="timeout",
default=10,
type=int,
help="Time in seconds plugin is allowed to run. (default: %(default)s)"
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
default=False,
help="Turn on verbosity."
)
# parse arguments
options = parser.parse_args()
# show command line options
if options.verbose:
# store password temporarily
password = options.password
# hide password from output
options.password = "xxxxxx"
# show command line options
print "command line options:\n%s\n" % (pformat(vars(options)))
# restore password
options.password = password
print " "
url_args = "{}{}?{}".format(str(options.url), str(options.components), str(options.filters))
try:
# make request
r = requests.get(
"%s" % (url_args),
verify=False,
auth=(options.username, options.password),
timeout=options.timeout
)
except Exception as message:
print "CRITICAL: Could not get data when querying the Infinibox (%s)" % (message)
exit(functions.CRITICAL)
# show raw results
if options.verbose:
print "Raw result:\n%s\n" % (r.text)
# exit if there was a bad status code
if r.status_code != 200:
print "CRITICAL: Could not get data when querying the Infinibox which returned (%d)" % (r.status_code)
exit(functions.CRITICAL)
# decode the json
result = r.json()
# show decoded json
if options.verbose:
print "Decoded json:\n%s\n" % (pformat(result))
state = 0
total = 0
i1 = 0
i2 = 0
i = 0
# build proper message
state, message = functions.build_response(state)
# override message and state if running in verbose mode
if options.verbose:
state = functions.UNKNOWN
message = "UNKNOWN:"
# evaluate returned data from API calls
outcome = result["result"]
# Define Graphite information
with open("settings.yaml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
for section in cfg:
if options.url.split(".")[1].find(section) != -1:
graphite_address = str(cfg[options.url.split(".")[1]]["graphite_address"])
graphite_port = cfg[options.url.split(".")[1]]["graphite_port"]
graphite_prefix = cfg[options.url.split(".")[1]]["graphite_prefix"]
graphite_timeout = cfg[options.url.split(".")[1]]["graphite_timeout"]
'''
print "graphite_address={}".format(graphite_address)
print "graphite_port={}".format(graphite_port)
print "graphite_prefix={}".format(graphite_prefix)
print "graphite_timeout={}".format(graphite_timeout)
print ""
'''
servername = options.servername
# Get data to send to Graphite
timestamp = str(time.time())[0:10]
read_iops = str(int(outcome[0]["read_ops"]))
read_bytes = str(int(outcome[0]["read_bytes"]))
read_latency = str(int(outcome[0]["read_latency"]))
write_iops = str(int(outcome[0]["write_ops"]))
write_bytes = str(int(outcome[0]["write_bytes"]))
write_latency = str(int(outcome[0]["write_latency"]))
total_iops = int(outcome[0]["read_ops"]) + int(outcome[0]["write_ops"])
total_bytes = int(outcome[0]["read_bytes"]) + int(outcome[0]["write_bytes"])
# Print out performance data
message = [["{}.{}.{} {} {}".format(graphite_prefix, servername, "read_iops", read_iops, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "read_bytes", read_bytes, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "read_latency", read_latency, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "write_iops", write_iops, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "write_bytes", write_bytes, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "write_latency", write_latency, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "total_iops", total_iops, timestamp)],
["{}.{}.{} {} {}".format(graphite_prefix, servername, "total_bytes", total_bytes, timestamp)]]
conn = socket.socket()
conn.connect((graphite_address, graphite_port))
while (i < len(message)):
msgOutput = message[i]
msgOutput = str(message[i]).strip("['']")
msgOutput = "{}\n".format(msgOutput)
print msgOutput
conn.sendall(msgOutput)
i = i + 1
conn.close()