check_nvme.cpp
4.69 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "check_nvme.h"
#include <regex>
const char *servicename = (const char*)"NVME";
int getNVMeAttrValue(string line) {
line = std::regex_replace(line, std::regex("\\s+"), " ");
line = std::regex_replace(line, std::regex("^ "), "");
return stoi(line.substr(line.find(":")+1));
}
std::string getNVMeAttrName(string line) {
size_t first = line.find_first_not_of(' ');
size_t last = line.find_first_of(':',first);
return line.substr(first,last-first);
}
int evalStatus(const char* disk, string *status) {
string output = "";
string line = "";
int rc = run_smartctl_cmd(string(SMARTCTL_CMD_ATTRS), disk,&output);
if(rc) {
cout << "Error reading SMART data from disk " << disk << endl;
exit(UNKN);
}
map<std::string,NVMeAttr> attrMap;
attrMap[AVAILABLE_SPARE] = spare;
attrMap[MEDIA_ERRORS] = errors;
attrMap[PERCENTAGE_USED] = used;
stringstream sstream;
sstream.str(output);
while(getline(sstream,line)) {
if(line == "") {
continue;
}
for(map<std::string,NVMeAttr>::iterator it = attrMap.begin(); it != attrMap.end(); ++it) {
std::string name = it->first;
if(getNVMeAttrName(line) == name) {
attrMap[name].value = getNVMeAttrValue(line);
}
}
}
int ret = OK;
*status = string(disk);
for(map<std::string, NVMeAttr>::iterator it = attrMap.begin(); it != attrMap.end(); ++it) {
NVMeAttr attr = it->second;
if(attr.value == -1) {
if(attr.optional) {
continue;
}
*status = string(disk) + " status UNKNOWN";
return UNKN;
}
int veredict = 0;
if(attr.lower_than) {
if(attr.value < attr.threshold_warn) {
veredict = WARN;
}
if(attr.threshold_crit != -1 && attr.value < attr.threshold_crit) {
veredict = CRIT;
}
} else {
if(attr.value > attr.threshold_warn) {
veredict = WARN;
}
if(attr.threshold_crit != -1 && attr.value > attr.threshold_crit) {
veredict = CRIT;
}
}
switch(veredict) {
case OK:
break;
case WARN:
if(ret == OK) {
ret = WARN;
}
break;
case CRIT:
ret = CRIT;
break;
}
*status += " " + attr.name + ":" + to_string(attr.value);
}
return ret;
}
int run_smartctl_cmd(const string command, const char* disk, string* output) {
int uid = getuid();
setreuid(0,0);
int rc = exec(command + disk,output);
setreuid(uid,0);
return rc;
}
void printVersion() {
cout << "check_nvme v" << VERSION << endl << endl;
}
void printHelp(bool longVersion) {
if(longVersion) {
printVersion();
cout << "Checks for pending, reallocated or uncorrectable sectors in NVMe disks" << endl << "WARNING: Requires the setuid bit to be set to run as root" << endl << endl;
printHelp(false);
cout << "Options:" << endl;
cout << " -h" << endl;
cout << " Print detailed help screen" << endl;
cout << " -V" << endl;
cout << " Print version information" << endl;
cout << " DISKS" << endl;
cout << " Disks for which to retrieve S.M.A.R.T data" << endl << endl;
return;
}
cout << "Usage: " << endl << "check_smart [-hV] DISKS..." << endl << endl;
}
void set_timeout(unsigned int sec) {
struct itimerval timer;
timer.it_value.tv_sec = sec;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
setitimer (ITIMER_VIRTUAL, &timer, 0);
struct sigaction sa;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGVTALRM, &sa, 0);
}
int main(int argc, char **argv) {
set_timeout(10);
int c;
while ((c = getopt (argc, argv, "Vh")) != -1) {
switch(c) {
case 'h':
printHelp(true);
return OK;
case 'V':
printVersion();
return OK;
case '?':
printHelp(false);
return UNKN;
}
}
if(argc == 1) {
cout << "No disks checked" << endl;
return 3;
}
string *results = new string[argc-1];
int returnCode = OK;
for(int i = 1; i < argc; i++) {
switch(evalStatus(argv[i],&(results[i-1]))) {
case OK:
break;
case WARN:
if(returnCode == OK) {returnCode = WARN;}
break;
case CRIT:
returnCode = CRIT;
break;
case UNKN:
returnCode = UNKN;
break;
}
}
cout << servicename;
if(returnCode == OK) {cout << " OK - disk status: ";}
else if(returnCode == WARN) {cout << " WARNING - disk status: ";}
else if(returnCode == CRIT) {cout << " CRITICAL - disk status: ";}
else if(returnCode == UNKN) {cout << " UNKNOWN - disk status: ";}
for(int i = 0; i < (argc-1); i++) {
cout << results[i];
if(i != (argc-2)) {cout << ", ";}
}
cout << endl;
return returnCode;
}