-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_storage_metrics.py
78 lines (64 loc) · 3.79 KB
/
generate_storage_metrics.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
#!/usr/bin/env python
#----------------------------------------------------------------------
# Copyright (c) 2013-2015 Raytheon BBN Technologies
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------
# Script to generate storage metrics (% used) of a given disk filesystem
# and write these to a database
# GEC22 demo visualizations
from generate_metrics import MetricGenerator
import time;
import optparse
import os
import subprocess
import sys
# Class to grab storage usage
class StorageMetricGenerator(MetricGenerator):
def __init__(self):
MetricGenerator.__init__(self)
# Add options to parser
def add_parser_options(self, parser):
MetricGenerator.add_parser_options(self, parser)
parser.add_option('--file_root', help="Name of disk file root", default="/")
# Set of metrics by name
def getMetricNames(self):
return ["used_storage", "free_storage"]
# Set of metrics by value
def getMetricValues(self):
st = os.statvfs(self._opts.file_root)
free = (st.f_bavail * st.f_frsize)
total = (st.f_blocks * st.f_frsize)
used = (st.f_blocks - st.f_bfree) * st.f_frsize
try:
percent_used = ret = (float(used) / total) * 100
except:
percent_used = 0
percent_used = int(percent_used)
return [percent_used, 100-percent_used]
# Run indefinitely, only adding new rows
def main():
smg = StorageMetricGenerator()
while True:
frequency = int(smg._opts.frequency)
smg.run()
time.sleep(frequency)
if __name__ == "__main__":
sys.exit(main())