-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiskcheckCW.py
executable file
·95 lines (75 loc) · 3.03 KB
/
diskcheckCW.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
#!/usr/bin/env python
#script for monitoring disk and partition usage
import time
import subprocess
from optparse import OptionParser
import boto
import boto.ec2.cloudwatch
import socket
# allow script to accept options - specifically partitions to ignore
parser = OptionParser()
parser.add_option('-i', '--ignore', type='string', action='append', dest='ignore', help='Partitions to be excluded from monitoring')
parser.add_option('-m', '--machine', type='string', action='store', dest='prodstage', help='Prod or Stage designaton, should be either <infrastructure-production> or <infrastructure-staging>')
(opts, args) = parser.parse_args()
ignore = opts.ignore
prodstage = opts.prodstage
arn = "none"
prodarn =
stagearn =
alarmfirsttime = True
metname = 'none'
if prodstage == "infrastructure-production":
arn = prodarn
metname = "infrastructure-prod-disk-fill"
elif prodstage == "infrastructure-staging":
arn = stagearn
metname = "infrastructure-staging-disk-fill"
else:
print "Machine option (-m) not properly set. Should be should be either <infrastructure-production> or <infrastructure-staging>"
quit()
while True:
# pass df to shell and capture output
df = subprocess.Popen(["df"], stdout=subprocess.PIPE)
output = df.communicate()[0]
disklist = output.split("\n")
# Gloabal variables
fillrates = {}
hostname = socket.gethostname()
# evaluate df output and extract the partition + fill rate
for lines in disklist[1:-1]:
part = lines.split()
usage_int = int(part[4].replace("%",""))
if usage_int > 0:
fillrates[part[0]] = usage_int
# pull out partitions that are to be ignored
if ignore:
print "Ignoring partitions %s" % (ignore)
for i in ignore:
if i in fillrates:
del fillrates[i]
else:
print "No partitions ignored"
# of the remaining parititons target the file system that is most full
dangerzone = max(fillrates, key=fillrates.get)
print "The File system on %s is currently %s percent full" % (dangerzone, fillrates[dangerzone])
#boto to call cloudwatch and post dangerzone as a metric
dimes = {'host':hostname, 'partition':dangerzone}
CWconnect = boto.ec2.cloudwatch.CloudWatchConnection(aws_access_key_id=' ', aws_secret_access_key=' ')
pushmetric = CWconnect.put_metric_data(namespace=str(prodstage), name='disk-fill', value=fillrates[dangerzone], unit='Percent', dimensions=dimes)
if alarmfirsttime == True:
alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
name= metname + "-" + hostname,
metric='disk-fill',
namespace=str(prodstage),
statistic='Maximum',
comparison='>=',
description='A disk partition is over 80% full, or has failed to check in.',
threshold=80,
period=3600,
evaluation_periods=1,
dimensions=dimes,
alarm_actions=[arn],
insufficient_data_actions=[arn])
CWconnect.put_metric_alarm(alarm)
alarmfirsttime = False
time.sleep(3600)