-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_smart.py
More file actions
executable file
·119 lines (98 loc) · 3.57 KB
/
check_smart.py
File metadata and controls
executable file
·119 lines (98 loc) · 3.57 KB
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
#!/usr/bin/env python2
# check_smart.py
#
# Copyright (C) 2015 Stefan Kauerauf <mail@stefankauerauf.de>
#
# This file is part of check_smart.
#
# check_smart is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# check_smart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU General Public License for more details. You should have received a copy of the GNU
# General Public License along with check_smart. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Stefan Kauerauf
import argparse
from pySMART import Device
import re, sys, os
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument('-D', '--device', default='/dev/sda')
argp.add_argument('-P', '--percent', default=5)
argp.add_argument('--html', default=False, action="store_true")
argp.add_argument('--noperf', default=False, action="store_true")
args = argp.parse_args()
device = Device(args.device)
attributes = []
for a in device.attributes:
if a:
attributes.append(a)
# fill table
# name, type, value, worst, warning, critical, raw, state, thresh
result = []
count_warning = 0
count_critical = 0
for a in attributes:
warning = None
critical = None
thresh = None
if a.type == "Pre-fail":
critical = float(a.thresh)
thresh = critical
warning = float(a.thresh) + ( (float(a.thresh) / 100) * float(args.percent) )
else:
critical = float(a.thresh) - ( (float(a.thresh) / 100) * float(args.percent) )
warning = float(a.thresh)
thresh = warning
state = 3
if float(a.value) >= warning and float(a.value) >= critical:
state = 0
elif float(a.value) < warning and float(a.value) >= critical:
state = 1
count_warning = count_warning + 1
elif float(a.value) <warning and float(a.value) < critical:
state = 2
count_critical = count_critical + 1
result.append([a.name, a.type, float(a.value), float(a.worst), float(warning), float(critical), a.raw, state, thresh])
# create output
attribute_num = len(result)
count_ok = len(result) - count_warning - count_critical
overall_state = 0
output = "OK: "
o_w = ""
o_c = ""
if count_warning > 0:
overall_state = 1
output = "WARNING: "
o_w = " - " + str(count_warning) + "/" + str(attribute_num) + " WARNING"
if count_critical > 0:
overall_state = 2
output = "CRITICAL: "
o_c = " - " + str(count_critical) + "/" + str(attribute_num) + " CRITICAL"
output = output + str(count_ok) + "/" + str(attribute_num) + " OK" + o_w + o_c
if args.html:
output = output + "</br>"
else:
output = output + "\n"
n_r = ["OK", "WARNING", "CRITICAL", "UNKNOWN"]
h_r = ["", "color:orange", "color:red", "color:blue"]
for a in result:
if args.html:
output = output + '<p style="' + h_r[a[7]] + '">'
output = output + a[0] + "(" + a[1] + ") " + n_r[a[7]] + " current: " + str(a[2]) + " worst: " + str(a[3]) + " thresh: " + str(a[8]) + " raw: " + str(a[6])
if args.html:
output = output + "</p>"
else:
output = output + "\n"
if args.noperf:
print(output)
sys.exit(overall_state)
# perfdata
output = output + "|"
for a in result:
output = output + a[0] + "=" + str(a[2]) + ";" + str(a[4]) + ";" + str(a[5]) + ";" + str(a[3]) + ";" + "" + " "
print(output)
sys.exit(overall_state)