File: check_ow.py

package info (click to toggle)
owfs 3.2p3+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,688 kB
  • sloc: ansic: 65,085; pascal: 5,957; tcl: 2,688; makefile: 1,357; python: 1,114; sh: 891; cs: 623; php: 600; perl: 587; java: 404; ruby: 289; cpp: 105; asm: 102; xml: 53
file content (75 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python

"""
Copyright (c) 2006 Peter Kropf. All rights reserved.

Nagios (http://nagios.org) plugin to check the value of a 1-wire sensor.
"""


import ownet
import sys
import os
from optparse import OptionParser
import socket


class nagios:
    ok       = (0, 'OK')
    warning  = (1, 'WARNING')
    critical = (2, 'CRITICAL')
    unknown  = (3, 'UNKNOWN')


# FIXME: need to add more help text in the init_string and sensor_path parameters
parser = OptionParser(usage='usage: %prog [options] server port sensor_path',
                      version='%prog ' + ownet.__version__)
parser.add_option('-v', dest='verbose', action='count',             help='multiple -v increases the level of debugging output.')
parser.add_option('-w', dest='warning',                 type='int', help='warning level.')
parser.add_option('-c', dest='critical',                type='int', help='critical level.')
parser.add_option('-f', dest='field',                               help='sensor field to be used for monitoring.', default='temperature')
options, args = parser.parse_args()

if len(args) != 3:
    print 'OW ' + nagios.unknown[1] + ' - ' + 'missing command line arguments'
    parser.print_help()
    sys.exit(nagios.unknown[0])

server = args[0]
port   = int(args[1])
path   = args[2]

try:
    s = ownet.Sensor(path, server=server, port=port)
except ownet.exUnknownSensor as ex:
    print 'OW ' + nagios.unknown[1] + ' - unknown sensor ' + str(ex)
    sys.exit(nagios.unknown[0])
except socket.error as ex:
    print 'OW ' + nagios.unknown[1] + ' - communication error ' + str(ex)
    sys.exit(nagios.unknown[0])

if options.verbose == 1:
    print s
elif options.verbose > 1:
    print s
    print 'entryList:', s.entryList()
    print 'sensorList:', s.sensorList()

if not hasattr(s, options.field):
    print 'OW ' + nagios.unknown[1] + ' - ' + 'unknown field: ' + options.field
    sys.exit(nagios.unknown[0])

val = getattr(s, options.field)

if val >= options.critical:
    status    = nagios.critical[1]
    exit_code = nagios.critical[0]
elif val >= options.warning:
    status    = nagios.warning[1]
    exit_code = nagios.warning[0]
else:
    status    = nagios.ok[1]
    exit_code = nagios.ok[0]

print 'OW %s - %s %s: %i| %s/%s=%i' % (status, path, options.field, val, path, options.field, val)
sys.exit(exit_code)