forked from innogames/igcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_disk_usage.py
executable file
·43 lines (33 loc) · 1.08 KB
/
linux_disk_usage.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
#!/usr/bin/env python
#
# igcollect - Linux disk usage
#
# Copyright (c) 2016, InnoGames GmbH
#
from __future__ import print_function
import socket, time, os, sys
def main():
mountpoints=[]
try:
with open('/proc/mounts', 'r') as file_descriptor:
for line in file_descriptor.readlines():
a,mountpoint,fstype,a = line.split(' ',3)
if fstype in ['ext2','ext3','ext4','xfs']: mountpoints.append(mountpoint)
except:
sys.exit(1)
now = str(int(time.time()))
hostname = socket.gethostname().replace('.', '_')
template = 'servers.' + hostname + '.system.fs.{0}.{1} {2} ' + now
for mp in mountpoints:
try:
stat = os.statvfs(mp)
except:
sys.exit(1)
used = stat.f_frsize * stat.f_blocks - stat.f_bfree * stat.f_bsize
size = stat.f_frsize * stat.f_blocks
if mp == '/': mp='rootfs'
mp = mp.replace('/','_').lstrip('_')
print(template.format(mp,'used',used))
print(template.format(mp,'size',size))
if __name__ == '__main__':
main()