forked from sleepka/zabbix-kubernetes-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s-stats.py
executable file
·131 lines (112 loc) · 5.54 KB
/
k8s-stats.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
#!/usr/bin/env python
import subprocess
import sys
import os
import json
import time
#Needs for azure-fix to ignore selfsigned ssl cert
import ssl
try:
import urllib.request as urllib2
except ImportError:
import urllib2
api_server = 'https://API_SERVER_URL'
token = 'TOKEN'
targets = ['pods','nodes','containers','deployments','apiservices','componentstatuses']
target = 'pods' if 'containers' == sys.argv[2] else sys.argv[2]
if 'pods' == target or 'nodes' == target or 'componentstatuses' == target:
api_req = '/api/v1/'+target
elif 'deployments' == target:
api_req = '/apis/apps/v1/'+target
elif 'apiservices' == target:
api_req = '/apis/apiregistration.k8s.io/v1/'+target
def rawdata(qtime=30):
if sys.argv[2] in targets:
tmp_file='/tmp/zbx-'+target+'.tmp'
tmp_file_exists=True if os.path.isfile(tmp_file) else False
if tmp_file_exists and (time.time()-os.path.getmtime(tmp_file)) <= qtime:
file = open(tmp_file,'r')
rawdata=file.read()
file.close()
else:
#azure-fix to ignore selfsigned ssl cert
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
req = urllib2.Request(api_server + api_req)
req.add_header('Authorization', 'Bearer ' + token)
#Use context with no ssl check for selfsigned certs
rawdata = urllib2.urlopen(req, context=ctx).read()
file = open(tmp_file,'wb')
file.write(rawdata)
file.close()
if not tmp_file_exists:
os.chmod(tmp_file, 0o666)
return rawdata
else:
return false
if sys.argv[2] in targets:
if 'discovery' == sys.argv[1]:
# discovery
result = {'data':[]}
data = json.loads(rawdata())
for item in data['items']:
if 'nodes' == sys.argv[2] or 'componentstatuses' == sys.argv[2] or 'apiservices' == sys.argv[2]:
result['data'].append({'{#NAME}':item['metadata']['name']})
elif 'containers' == sys.argv[2]:
for cont in item['spec']['containers']:
result['data'].append({'{#NAME}':item['metadata']['name'],'{#NAMESPACE}':item['metadata']['namespace'],'{#CONTAINER}':cont['name']})
else:
result['data'].append({'{#NAME}':item['metadata']['name'],'{#NAMESPACE}':item['metadata']['namespace']})
print(json.dumps(result))
elif 'stats' == sys.argv[1]:
# stats
data = json.loads(rawdata(100))
if 'pods' == sys.argv[2] or 'deployments' == sys.argv[2]:
for item in data['items']:
if item['metadata']['namespace'] == sys.argv[3] and item['metadata']['name'] == sys.argv[4]:
if 'statusPhase' == sys.argv[5]:
print(item['status']['phase'])
elif 'statusReason' == sys.argv[5]:
if 'reason' in item['status']:
print (item['status']['reason'])
elif 'statusReady' == sys.argv[5]:
for status in item['status']['conditions']:
if status['type'] == 'Ready' or (status['type'] == 'Available' and 'deployments' == sys.argv[2]):
print(status['status'])
break
elif 'containerReady' == sys.argv[5]:
for status in item['status']['containerStatuses']:
if status['name'] == sys.argv[6]:
for state in status['state']:
if state == 'terminated':
if status['state']['terminated']['reason'] == 'Completed':
print('True')
break
else:
print(status['ready'])
break
elif 'containerRestarts' == sys.argv[5]:
for status in item['status']['containerStatuses']:
if status['name'] == sys.argv[6]:
print(status['restartCount'])
break
elif 'Replicas' == sys.argv[5]:
print (item['spec']['replicas'])
elif 'updatedReplicas' == sys.argv[5]:
print (item['status']['updatedReplicas'])
break
if 'nodes' == sys.argv[2] or 'apiservices' == sys.argv[2]:
for item in data['items']:
if item['metadata']['name'] == sys.argv[3]:
for status in item['status']['conditions']:
if status['type'] == sys.argv[4]:
print(status['status'])
break
elif 'componentstatuses' == sys.argv[2]:
for item in data['items']:
if item['metadata']['name'] == sys.argv[3]:
for status in item['conditions']:
if status['type'] == sys.argv[4]:
print(status['status'])
break