-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
executable file
·144 lines (126 loc) · 4.29 KB
/
init.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
#!/usr/bin/python
"""Thios is Jenkins HTTP check remote modue."""
import sys
import os
from datetime import datetime
import requests
from datadog import initialize
from datadog import api
import yaml
# Parse config file
with open("/app/cfg/config.yaml", 'r') as stream:
try:
config = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
sys.exit(1)
except IOError as e:
print(("Config file not found: {0}").format(e))
sys.exit(1)
cfg_metrics = config['metrics']
api_key = os.environ['DATADOG_API_KEY']
app_key = os.environ['DATADOG_APP_KEY']
metrics_token = os.environ['JENKINS_METRICS_TOKEN']
jenkins_uri = ("https://{0}").format(config['host'])
options = {
'api_key': api_key,
'app_key': app_key
}
initialize(**options)
metrics_url = ("{0}/metrics/{1}/metrics").format(jenkins_uri, metrics_token)
ping_url = ("{0}/metrics/{1}/ping").format(jenkins_uri, metrics_token)
healthcheck_url = ("{0}/metrics/{1}/healthcheck").format(jenkins_uri, metrics_token) # noqa
# populate mentrics tags
tags = []
if 'tags' in config:
for tag in config['tags']:
tags.append(("{0}:{1}").format(tag['name'], tag['value']))
def host_ping():
"""Ping Jenkins and report its alive."""
ping_tag = tags
ping_tag.append("type:healthchek")
try:
request = requests.get(ping_url)
pingcheck = request.content.rstrip()
except requests.exceptions.RequestException as e:
api.Metric.send(
metric='jenkins.healthcheck.ping',
host=config['host'],
points=1,
tags=ping_tag,
type='gauge'
)
print(("Jenkins communication error {0}").format(datetime.utcnow().isoformat(), e)) # noqa
sys.exit(1)
if request.ok and pingcheck == 'pong':
api.Metric.send(
metric='jenkins.healthcheck.ping',
host=config['host'],
points=0,
tags=ping_tag,
type='gauge'
)
print(("{0} Jenkins ping").format(datetime.utcnow().isoformat()))
def report_healthcheck():
"""Send Healthcheck."""
# Request metrics data from Jenkins server
health_tag = tags
health_tag.append("type:healthchek")
try:
request = requests.get(healthcheck_url)
healthcheck = request.json()
except requests.exceptions.RequestException as e:
print(("Jenkins communication error {0}").format(datetime.utcnow().isoformat(), e)) # noqa
sys.exit(1)
# populate mentrics tags
for check in healthcheck.keys():
if healthcheck[check]['healthy'] is True:
check_value = 0
else:
check_value = 1
api.Metric.send(
metric=("jenkins.healthcheck.{0}").format(check),
host=config['host'],
points=check_value,
tags=health_tag,
type='gauge'
)
print(("{0} Jenkins healthcheck").format(datetime.utcnow().isoformat()))
def report_metrics():
"""Report metrics."""
metrics_tag = tags
metrics_tag.append("type:metrics")
# Request metrics data from Jenkins server
try:
request = requests.get(metrics_url)
metrics = request.json()
except requests.exceptions.RequestException as e:
print(("Jenkins communication error {0}").format(datetime.utcnow().isoformat(), e)) # noqa
sys.exit(1)
# Send gauges
if 'gauges' in cfg_metrics:
if 'gauges' in metrics:
for gauge in cfg_metrics['gauges']:
api.Metric.send(
metric=gauge,
host=config['host'],
points=metrics['gauges'][gauge]['value'],
tags=metrics_tag,
type='gauge'
)
print(("{0} Jenkins gauges").format(datetime.utcnow().isoformat()))
# Send meters
if 'meters' in cfg_metrics:
if 'meters' in metrics:
for meter in cfg_metrics['meters']:
api.Metric.send(
metric=meter,
host=config['host'],
points=metrics['meters'][meter]['count'],
tags=metrics_tag,
type='counter'
)
print(("{0} Jenkins meters").format(datetime.utcnow().isoformat()))
host_ping()
report_healthcheck()
report_metrics()