This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitoring.py
executable file
·119 lines (105 loc) · 5.11 KB
/
monitoring.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
#!/usr/bin/python
'''
File: monitoring.py
Description: Sends Squid metrics to T-NOVA VIM infrastructure.
Metrics:
- Number of HTTP requests received by Squid
- Cache hits percentage of all requests for the last 5 minutes
- Cache hits percentage of bytes sent for the last 5 minutes
- Memory hits percentage for the last 5 minutes (hits that are logged as TCP_MEM_HIT)
- Disk hits percentage for the last 5 minutes (hits that are logged as TCP_HIT)
- Cache disk utilization
- Cache memory utilization
- Number of users accessing the proxy
- CPU usage for the last 5 minutes
'''
import argparse
from influxdb import InfluxDBClient
from parsesquid import Squidclient
import time
import sys
from datetime import datetime
import os
uuid_file = '/etc/collectd/instance_uuid'
fixed_uuid = '03ed10fb-39d0-455e-bd5c-882552baaef0'
def get_uuid():
if os.path.isfile(uuid_file):
with open(uuid_file, 'r') as f:
return f.readline().rstrip()
else:
return fixed_uuid
''' Sends metrics to influxdb at specified host and port '''
class Monitoring(object):
def __init__(self, host='monitoring', port=8086, username='stats_user', password='tnova', db_name='statsdb'):
self.client = InfluxDBClient(host, port, username, password, db_name)
def send_metric(self, name, value):
json_body = [
{
"measurement": name,
"tags": {
"host": get_uuid()
},
"fields": {
"value": value
}
}
]
self.client.write_points(json_body)
if __name__ == '__main__':
dir_path = os.path.dirname(os.path.abspath(__file__))
shell = Monitoring()
squid = Squidclient()
f = open(os.path.join(dir_path, 'logs.txt'), 'a')
process = squid.execute()
if process:
current_http = squid.parse(process, "Number of HTTP requests received:")
hits_percentage = squid.parse(process, "Hits as % of all requests:", ",", False)
hits_percentage_bytes = squid.parse(process, "Hits as % of bytes sent:", ",", False)
memory_hits = squid.parse(process, "Memory hits as % of hit requests:", ",", False)
disk_hits = squid.parse(process, "Disk hits as % of hit requests:", ",", False)
cache_disk_utilization = squid.parse(process, "Storage Swap capacity:", ",")
cache_memory_utilization = squid.parse(process, "Storage Mem capacity:", ",")
number_of_users = squid.parse(process, "Number of clients accessing cache:")
cpu_usage = squid.parse(process, "CPU Usage:")
if all((current_http, hits_percentage, memory_hits, disk_hits, cache_disk_utilization, cache_memory_utilization, number_of_users, cpu_usage)):
current_http = int(current_http)
file = open(os.path.join(dir_path, 'state.txt'), 'r')
previous_http = int(file.read())
file.close()
if previous_http == 0:
previous_http = current_http
file = open(os.path.join(dir_path, 'state.txt'), 'w')
file.write(str(current_http))
file.close()
diff_http = current_http - previous_http # gets the number of HTTP requests since the previous measurement
if diff_http < 0:
diff_http = 0
shell.send_metric('httpnum', int(diff_http))
shell.send_metric('hits', float(hits_percentage[:-1]))
shell.send_metric('hits_bytes', float(hits_percentage_bytes[:-1]))
shell.send_metric('memoryhits', float(memory_hits[:-1]))
shell.send_metric('diskhits', float(disk_hits[:-1]))
shell.send_metric('cachediskutilization', float(cache_disk_utilization[:-6]))
shell.send_metric('cachememkutilization', float(cache_memory_utilization[:-6]))
shell.send_metric('usernum', int(number_of_users))
shell.send_metric('cpuusage', float(cpu_usage[:-1]))
f.write("time: "+datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"\n")
f.write("Number of HTTP requests received: "+str(diff_http)+"\n")
f.write("Hits as % of all requests: "+hits_percentage[:-1]+"\n")
f.write("Hits as % of bytes sent: "+hits_percentage_bytes[:-1]+"\n")
f.write("Memory hits as % of hit requests: "+memory_hits[:-1]+"\n")
f.write("Disk hits as % of hit requests: "+disk_hits[:-1]+"\n")
f.write("Storage Swap capacity: "+cache_disk_utilization[:-6]+"\n")
f.write("Storage Mem capacity: "+cache_memory_utilization[:-6]+"\n")
f.write("Number of clients accessing cache:"+number_of_users+"\n")
f.write("CPU Usage: "+cpu_usage[:-1]+"\n")
f.write("----------------------\n")
else:
f.write("time: "+datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"\n")
f.write('Unable to send metrics\n')
f.write("----------------------\n")
else:
f.write("time: "+datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"\n")
f.write('Unable to send metrics\n')
f.write("----------------------\n")
f.close()