This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiostat.py
executable file
·96 lines (76 loc) · 2.41 KB
/
iostat.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
#!/usr/bin/env python3
"""Author: Olivier van der Toorn <[email protected]>
Description: Pusher of iostat data to Zabbix.
"""
import threading
import subprocess
import select
import queue
import signal
import sys
import time
import pdb
import re
import socket
import yaml
from pyzabbix import ZabbixMetric, ZabbixSender
whitespace = re.compile(r'\ +')
stats_queue = queue.Queue()
control_queue = queue.Queue()
def producer():
"""Reads values from the iostat process and pushes them to the queue.
"""
command = ['iostat', '-Nxy', str(config['interval'])]
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
if control_queue.empty() is False:
command = control_queue.get()
if command == 'SIGINT':
process.terminate()
break
if len(select.select([process.stdout], [], [], 0)[0]) > 0:
lines = str(process.stdout.readline(), 'utf-8').split('\n')
for line in lines:
if line.count('Device') > 0:
continue
parts = whitespace.split(line)
if len(parts) == 23:
disk = parts[0]
data = {}
for key, value in config['keys'].items():
data[key] = parts[value]
stats_queue.put((disk, data))
else:
time.sleep(1)
control_queue.task_done()
def signal_handler(signum, frame):
"""Controls the SIGINT.
"""
control_queue.put('SIGINT')
control_queue.join()
sys.exit()
def main():
hostname = socket.gethostname()
zabbix = ZabbixSender(zabbix_server=config['zabbix_server'])
while True:
packets = []
while stats_queue.empty() is False:
disk, data = stats_queue.get()
packets.extend(
[ZabbixMetric(hostname, f'iostat[{disk},{key}]', value)
for key, value in data.items()])
if packets:
result = zabbix.send(packets)
print(packets, result)
time.sleep(1)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
path = 'iostat.yml'
with open(path, 'r') as config_file:
config = yaml.load(config_file, Loader=yaml.Loader)
t = threading.Thread(target=producer)
t.daemon = True
t.start()
# del t
main()