-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcheck_jenkins.py
More file actions
146 lines (128 loc) · 4.86 KB
/
check_jenkins.py
File metadata and controls
146 lines (128 loc) · 4.86 KB
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
#!/usr/bin/env python
"""
Author: Tony Ling
Uses Jenkins HTTP API for server metrics and parses the output
Metrics supported: mode, quietingDown, useCrumbs, views, buildQueue, executorsUtilization
Warnings if metric value is less than threshold: views
Warnings if metric value is greater than threshold: executorsUtilization, buildQueue
Example Usage:
python check_jenkins.py -t executorsUtilization -H localhost -P 8080 -W 80 -C 90
Created on: 10/31/14
"""
import sys
import urllib2
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', help='Host location, default=localhost', default='localhost')
parser.add_argument('-P', '--port', help='Port number, default=8080', default='8080')
parser.add_argument('-W', '--warning', help='Warning threshold', default=None, type=float)
parser.add_argument('-C', '--critical', help='Critical theshold', default=None, type=float)
parser.add_argument('-t', '--metric', help='Metric to choose', default=None, required=True)
args = parser.parse_args()
def check_thresholds():
if (args.critical and args.critical < 0):
print "Error: -C Critical cannot be negative"
sys.exit(status_code)
elif (args.warning and args.warning < 0):
print "Error: -W Warning cannot be negative"
sys.exit(status_code)
def cmp_less(msg, value):
check_thresholds()
if (args.critical and float(value) <= args.critical):
print "CRITICAL - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 2
elif (args.warning and float(value) <= args.warning):
print "WARNING - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 1
else:
print "OK - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 0
sys.exit(status_code)
def cmp_greater(msg, value):
check_thresholds()
if (args.critical and float(value) >= args.critical):
print "CRITICAL - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 2
elif (args.warning and float(value) >= args.warning):
print "WARNING - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 1
else:
print "OK - " + msg + ": " + str(value) + "|" + args.metric + "=" + str(value)
status_code = 0
sys.exit(status_code)
def get_mode():
metrics = get_jenkins_metrics("api/json")
status = metrics[args.metric]
if status == "NORMAL":
print "OK - Server mode: " + status + "|" + args.metric + "=" + status
status_code = 0
sys.exit(status_code)
else:
print "WARNING - Server mode: " + status + "|" + args.metric + "=" + status
status_code = 1
sys.exit(status_code)
def get_quietingDown():
metrics = get_jenkins_metrics("api/json")
status = str(metrics[args.metric])
print "OK - Server is quieting down: " + status + "|" + args.metric + "=" + status
status_code = 0
sys.exit(status_code)
def get_useCrumbs():
metrics = get_jenkins_metrics("api/json")
status = str(metrics[args.metric])
print "OK - Server is using crumbs: " + status + "|" + args.metric + "=" + status
status_code = 0
sys.exit(status_code)
def get_views():
metrics = get_jenkins_metrics("api/json")
value = len(metrics[args.metric])
msg = "Number of views"
cmp_less(msg, value)
def get_buildQueue():
metrics = get_jenkins_metrics("queue/api/json")
value = len(metrics["items"])
msg = "Number of build queue items"
cmp_greater(msg, value)
def get_executorsUtilization():
metrics = get_jenkins_metrics("computer/api/json")
busy = int(metrics["busyExecutors"])
total = float(metrics["totalExecutors"])
percent = busy/total
msg = "Executors Utilization (%)"
cmp_greater(msg, percent)
def get_jenkins_metrics(url):
try:
output = urllib2.urlopen("http://{host}:{port}/{url}".format(host=args.host, port=args.port, url=url))
jenkins_metrics = json.loads(output.read())
except KeyError:
print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric)
str_metrics = "Supported Metrics: ["
for metric in metrics:
str_metrics += ("'" + metric + "',")
print str_metrics[:-1] + "]"
sys.exit(status_code)
except urllib2.URLError:
print "Error: Connection Refused - Check host and port arguments"
sys.exit(status_code)
return jenkins_metrics
# metrics dict keys are script metric -t arguments
# key values are functions called when the keys are matched
metrics = { "mode": get_mode,
"quietingDown": get_quietingDown,
"useCrumbs": get_useCrumbs,
"views": get_views,
"buildQueue": get_buildQueue,
"executorsUtilization": get_executorsUtilization
}
# Program entry starts here
status_code = 3
try:
metrics[args.metric]()
except KeyError:
print "Error: Unrecognized/unsupported metric '{_metric}'".format(_metric=args.metric)
str_metrics = "Supported Metrics: ["
for metric in metrics:
str_metrics += ("'" + metric + "',")
print str_metrics[:-1] + "]"
sys.exit(status_code)