-
Notifications
You must be signed in to change notification settings - Fork 2
/
arducheck.py
executable file
·167 lines (139 loc) · 6.31 KB
/
arducheck.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/env python
###############################################################################
# Thalhalla ardushipper code
#
# Notes
# - The RHEL boxes I work on are currently limited to Python 2.6.6, hence the
# use of (deprecated) optparse. If I can ever get them all updated to
# Python 2.7 (or better yet, 3.3), I'll switch to argparse
# - This template runs in 2.6-3.3. Any changes made will need to be appropriate
# to the Python distro you want to use
#
###############################################################################
__author__ = 'Josh Cox'
__version__= 0.1
from optparse import OptionParser, OptionGroup
import logging as log
import serial
import syslog
from time import sleep
from string import Template
## These will override any args passed to the script normally. Comment out after testing.
#testargs = '--help'
#testargs = '--version'
#testargs = '-vvv'
def main():
## Parse command-line arguments
args, args2 = parse_args()
""" Main plugin logic goes here """
log.debug('Main Logic')
nagiosExitCode = 0
nagiosExitMessage = 'OK: Humidity is normal'
log.debug('Starting Serial')
ser = serial.Serial(args.device, 115200, timeout=15)
# a write to serial is necessary
# to kick off arduino
ser.write("hello")
#log.debug('Opening Syslog')
#syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_DAEMON)
count = 0
bsq26humidity = -1;
log.debug('Opening While loop')
while True:
ser.write("1")
count = 1;
rawdata = ser.readline()
buff1 = "%s" % rawdata.split(b'\0',1)[0]
data = "%s" % buff1.strip()
log.debug('data:')
log.debug(data)
if "BSQ26-ENDTRANSMISSION" in data:
count+= 1
if "BSQ26-Humidity" in data:
bsq26humidity = data.split(' ')[1]
log.debug('Humidity:')
log.debug(data)
if count > 1:
log.debug('break')
break
if count > 0:
if not data.isspace():
if len(data) > 0:
log.debug('Got:', data)
#syslog.syslog(str(data))
sleep(0.5)
ser.write("1")
ser.close()
floatHumidity = float( bsq26humidity )
nagiosExitMessageTemplate = Template('$CONDITION: Humidity is $POSITION $OVERUNDER the threshold of $THRESH at $VALUE')
# nagiosExitMessage = 'OK: Humidity is normal'
# nagiosExitMessage = 'OK: Humidity', floatHumidity 'is normal'
nagiosExitMessage = nagiosExitMessageTemplate.substitute(CONDITION='OK', POSITION='is normal', OVERUNDER='under', THRESH=args.warn, VALUE=floatHumidity)
if floatHumidity < args.lowarn:
nagiosExitCode = 1
nagiosExitMessage = nagiosExitMessageTemplate.substitute(CONDITION='WARN', POSITION='is low', OVERUNDER='under', THRESH=args.lowarn, VALUE=floatHumidity)
if floatHumidity < args.locrit:
nagiosExitCode = 2
nagiosExitMessage = 'CRITICAL: Humidity', floatHumidity ,'is extremely low, under threshold', args.locrit
if floatHumidity > args.warn:
nagiosExitCode = 1
nagiosExitMessage = nagiosExitMessageTemplate.substitute(CONDITION='WARN', POSITION='is high', OVERUNDER='over', THRESH=args.warn, VALUE=floatHumidity)
if floatHumidity > args.crit:
nagiosExitCode = 2
nagiosExitMessage = 'CRITICAL: Humidity', floatHumidity ,'is extremely high, over threshold', args.crit
if floatHumidity == -1:
nagiosExitCode = 3
nagiosExitMessage = 'UNKNOWN: Humidity is unknown', floatHumidity
""" Main plugin logic ends here """
## Uncomment to test logging levels against verbosity settings
# log.debug('debug message')
# log.info('info message')
# log.warning('warning message')
# log.error('error message')
# log.critical('critical message')
# log.fatal('fatal message')
#gtfo(0)
gtfo(nagiosExitCode, nagiosExitMessage)
def parse_args():
""" Parse command-line arguments """
parser = OptionParser(usage='usage: %prog [-v|vv|vvv] [options]',
version='{0}: v.{1} by {2}'.format('%prog', __version__, __author__))
## Verbosity (want this first, so it's right after --help and --version)
parser.add_option('-v', help='Set verbosity level',
action='count', default=0, dest='v')
## CLI arguments specific to this script
group = OptionGroup(parser,'Plugin Options')
group.add_option('-x', '--extra', help='Your option here',
default=None)
## Common CLI arguments
parser.add_option('-c', '--critical', help='Set the critical threshold. Default: %(default)s',
default=85, type=float, dest='crit', metavar='##')
parser.add_option('-w', '--warning', help='Set the warning threshold. Default: %(default)s',
default=75, type=float, dest='warn', metavar='##')
parser.add_option('-l', '--locrit', help='Set the low critical threshold. Default: %(default)s',
default=4, type=float, dest='locrit', metavar='##')
parser.add_option('-n', '--lowarn', help='Set the low warning threshold. Default: %(default)s',
default=8, type=float, dest='lowarn', metavar='##')
parser.add_option('-d', '--device', help='Set the device to listen to. Default: %(default)s',
default='/dev/USB0', dest='device', metavar='##')
parser.add_option_group(group)
## Try to parse based on the testargs variable. If it doesn't exist, use args
try:
args, args2 = parser.parse_args(testargs.split())
except NameError:
args, args2 = parser.parse_args()
## Set the logging level based on the -v arg
log.getLogger().setLevel([log.ERROR, log.WARN, log.INFO, log.DEBUG][args.v])
log.debug('Parsed arguments: {0}'.format(args))
log.debug('Other arguments: {0}'.format(args2))
return args, args2
def gtfo(exitcode, message=''):
""" Exit gracefully with exitcode and (optional) message """
log.debug('Exiting with status {0}. Message: {1}'.format(exitcode, message))
if message:
print(message)
exit(exitcode)
if __name__ == '__main__':
## Initialize logging before hitting main, in case we need extra debuggability
log.basicConfig(level=log.DEBUG, format='%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
main()