check_overcr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*****************************************************************************
  2. *
  3. * Nagios check_overcr plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_overcr plugin
  11. *
  12. * This plugin attempts to contact the Over-CR collector daemon running on the
  13. * remote UNIX server in order to gather the requested system information.
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_overcr";
  32. const char *copyright = "2000-2007";
  33. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  34. #include "common.h"
  35. #include "netutils.h"
  36. #include "utils.h"
  37. enum checkvar {
  38. NONE,
  39. LOAD1,
  40. LOAD5,
  41. LOAD15,
  42. DPU,
  43. PROCS,
  44. NETSTAT,
  45. UPTIME
  46. };
  47. enum {
  48. PORT = 2000
  49. };
  50. char *server_address = NULL;
  51. int server_port = PORT;
  52. double warning_value = 0L;
  53. double critical_value = 0L;
  54. int check_warning_value = FALSE;
  55. int check_critical_value = FALSE;
  56. enum checkvar vars_to_check = NONE;
  57. int cmd_timeout = 1;
  58. int netstat_port = 0;
  59. char *disk_name = NULL;
  60. char *process_name = NULL;
  61. char send_buffer[MAX_INPUT_BUFFER];
  62. int process_arguments (int, char **);
  63. void print_usage (void);
  64. void print_help (void);
  65. int
  66. main (int argc, char **argv)
  67. {
  68. int result = STATE_UNKNOWN;
  69. char recv_buffer[MAX_INPUT_BUFFER];
  70. char temp_buffer[MAX_INPUT_BUFFER];
  71. char *temp_ptr = NULL;
  72. int found_disk = FALSE;
  73. unsigned long percent_used_disk_space = 100;
  74. double load;
  75. double load_1min;
  76. double load_5min;
  77. double load_15min;
  78. int port_connections = 0;
  79. int processes = 0;
  80. double uptime_raw_hours;
  81. int uptime_raw_minutes = 0;
  82. int uptime_days = 0;
  83. int uptime_hours = 0;
  84. int uptime_minutes = 0;
  85. setlocale (LC_ALL, "");
  86. bindtextdomain (PACKAGE, LOCALEDIR);
  87. textdomain (PACKAGE);
  88. /* Parse extra opts if any */
  89. argv=np_extra_opts (&argc, argv, progname);
  90. if (process_arguments (argc, argv) == ERROR)
  91. usage4 (_("Could not parse arguments"));
  92. /* initialize alarm signal handling */
  93. signal (SIGALRM, socket_timeout_alarm_handler);
  94. /* set socket timeout */
  95. alarm (socket_timeout);
  96. result = process_tcp_request2 (server_address,
  97. server_port,
  98. send_buffer,
  99. recv_buffer,
  100. sizeof (recv_buffer));
  101. switch (vars_to_check) {
  102. case LOAD1:
  103. case LOAD5:
  104. case LOAD15:
  105. if (result != STATE_OK)
  106. die (result, _("Unknown error fetching load data\n"));
  107. temp_ptr = (char *) strtok (recv_buffer, "\r\n");
  108. if (temp_ptr == NULL)
  109. die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
  110. else
  111. load_1min = strtod (temp_ptr, NULL);
  112. temp_ptr = (char *) strtok (NULL, "\r\n");
  113. if (temp_ptr == NULL)
  114. die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
  115. else
  116. load_5min = strtod (temp_ptr, NULL);
  117. temp_ptr = (char *) strtok (NULL, "\r\n");
  118. if (temp_ptr == NULL)
  119. die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
  120. else
  121. load_15min = strtod (temp_ptr, NULL);
  122. switch (vars_to_check) {
  123. case LOAD1:
  124. strcpy (temp_buffer, "1");
  125. load = load_1min;
  126. break;
  127. case LOAD5:
  128. strcpy (temp_buffer, "5");
  129. load = load_5min;
  130. break;
  131. default:
  132. strcpy (temp_buffer, "15");
  133. load = load_15min;
  134. break;
  135. }
  136. if (check_critical_value == TRUE && (load >= critical_value))
  137. result = STATE_CRITICAL;
  138. else if (check_warning_value == TRUE && (load >= warning_value))
  139. result = STATE_WARNING;
  140. die (result,
  141. _("Load %s - %s-min load average = %0.2f"),
  142. state_text(result),
  143. temp_buffer,
  144. load);
  145. break;
  146. case DPU:
  147. if (result != STATE_OK)
  148. die (result, _("Unknown error fetching disk data\n"));
  149. for (temp_ptr = (char *) strtok (recv_buffer, " ");
  150. temp_ptr != NULL;
  151. temp_ptr = (char *) strtok (NULL, " ")) {
  152. if (!strcmp (temp_ptr, disk_name)) {
  153. found_disk = TRUE;
  154. temp_ptr = (char *) strtok (NULL, "%");
  155. if (temp_ptr == NULL)
  156. die (STATE_CRITICAL, _("Invalid response from server\n"));
  157. else
  158. percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
  159. break;
  160. }
  161. temp_ptr = (char *) strtok (NULL, "\r\n");
  162. }
  163. /* error if we couldn't find the info for the disk */
  164. if (found_disk == FALSE)
  165. die (STATE_CRITICAL,
  166. "CRITICAL - Disk '%s' non-existent or not mounted",
  167. disk_name);
  168. if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
  169. result = STATE_CRITICAL;
  170. else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
  171. result = STATE_WARNING;
  172. die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
  173. break;
  174. case NETSTAT:
  175. if (result != STATE_OK)
  176. die (result, _("Unknown error fetching network status\n"));
  177. else
  178. port_connections = strtod (recv_buffer, NULL);
  179. if (check_critical_value == TRUE && (port_connections >= critical_value))
  180. result = STATE_CRITICAL;
  181. else if (check_warning_value == TRUE && (port_connections >= warning_value))
  182. result = STATE_WARNING;
  183. die (result,
  184. _("Net %s - %d connection%s on port %d"),
  185. state_text(result),
  186. port_connections,
  187. (port_connections == 1) ? "" : "s",
  188. netstat_port);
  189. break;
  190. case PROCS:
  191. if (result != STATE_OK)
  192. die (result, _("Unknown error fetching process status\n"));
  193. temp_ptr = (char *) strtok (recv_buffer, "(");
  194. if (temp_ptr == NULL)
  195. die (STATE_CRITICAL, _("Invalid response from server\n"));
  196. temp_ptr = (char *) strtok (NULL, ")");
  197. if (temp_ptr == NULL)
  198. die (STATE_CRITICAL, _("Invalid response from server\n"));
  199. else
  200. processes = strtod (temp_ptr, NULL);
  201. if (check_critical_value == TRUE && (processes >= critical_value))
  202. result = STATE_CRITICAL;
  203. else if (check_warning_value == TRUE && (processes >= warning_value))
  204. result = STATE_WARNING;
  205. die (result,
  206. _("Process %s - %d instance%s of %s running"),
  207. state_text(result),
  208. processes,
  209. (processes == 1) ? "" : "s",
  210. process_name);
  211. break;
  212. case UPTIME:
  213. if (result != STATE_OK)
  214. return result;
  215. uptime_raw_hours = strtod (recv_buffer, NULL);
  216. uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
  217. if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
  218. result = STATE_CRITICAL;
  219. else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
  220. result = STATE_WARNING;
  221. uptime_days = uptime_raw_minutes / 1440;
  222. uptime_raw_minutes %= 1440;
  223. uptime_hours = uptime_raw_minutes / 60;
  224. uptime_raw_minutes %= 60;
  225. uptime_minutes = uptime_raw_minutes;
  226. die (result,
  227. _("Uptime %s - Up %d days %d hours %d minutes"),
  228. state_text(result),
  229. uptime_days,
  230. uptime_hours,
  231. uptime_minutes);
  232. break;
  233. default:
  234. die (STATE_UNKNOWN, _("Nothing to check!\n"));
  235. break;
  236. }
  237. }
  238. /* process command-line arguments */
  239. int
  240. process_arguments (int argc, char **argv)
  241. {
  242. int c;
  243. int option = 0;
  244. static struct option longopts[] = {
  245. {"port", required_argument, 0, 'p'},
  246. {"timeout", required_argument, 0, 't'},
  247. {"critical", required_argument, 0, 'c'},
  248. {"warning", required_argument, 0, 'w'},
  249. {"variable", required_argument, 0, 'v'},
  250. {"hostname", required_argument, 0, 'H'},
  251. {"version", no_argument, 0, 'V'},
  252. {"help", no_argument, 0, 'h'},
  253. {0, 0, 0, 0}
  254. };
  255. /* no options were supplied */
  256. if (argc < 2)
  257. return ERROR;
  258. /* backwards compatibility */
  259. if (!is_option (argv[1])) {
  260. server_address = argv[1];
  261. argv[1] = argv[0];
  262. argv = &argv[1];
  263. argc--;
  264. }
  265. for (c = 1; c < argc; c++) {
  266. if (strcmp ("-to", argv[c]) == 0)
  267. strcpy (argv[c], "-t");
  268. else if (strcmp ("-wv", argv[c]) == 0)
  269. strcpy (argv[c], "-w");
  270. else if (strcmp ("-cv", argv[c]) == 0)
  271. strcpy (argv[c], "-c");
  272. }
  273. while (1) {
  274. c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
  275. &option);
  276. if (c == -1 || c == EOF || c == 1)
  277. break;
  278. switch (c) {
  279. case '?': /* print short usage statement if args not parsable */
  280. usage5 ();
  281. case 'h': /* help */
  282. print_help ();
  283. exit (STATE_OK);
  284. case 'V': /* version */
  285. print_revision (progname, NP_VERSION);
  286. exit (STATE_OK);
  287. case 'H': /* hostname */
  288. server_address = optarg;
  289. break;
  290. case 'p': /* port */
  291. if (is_intnonneg (optarg))
  292. server_port = atoi (optarg);
  293. else
  294. die (STATE_UNKNOWN,
  295. _("Server port an integer\n"));
  296. break;
  297. case 'v': /* variable */
  298. if (strcmp (optarg, "LOAD") == 0) {
  299. strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
  300. if (strcmp (optarg, "LOAD1") == 0)
  301. vars_to_check = LOAD1;
  302. else if (strcmp (optarg, "LOAD5") == 0)
  303. vars_to_check = LOAD5;
  304. else if (strcmp (optarg, "LOAD15") == 0)
  305. vars_to_check = LOAD15;
  306. }
  307. else if (strcmp (optarg, "UPTIME") == 0) {
  308. vars_to_check = UPTIME;
  309. strcpy (send_buffer, "UPTIME\r\n");
  310. }
  311. else if (strstr (optarg, "PROC") == optarg) {
  312. vars_to_check = PROCS;
  313. process_name = strscpy (process_name, optarg + 4);
  314. sprintf (send_buffer, "PROCESS %s\r\n", process_name);
  315. }
  316. else if (strstr (optarg, "NET") == optarg) {
  317. vars_to_check = NETSTAT;
  318. netstat_port = atoi (optarg + 3);
  319. sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
  320. }
  321. else if (strstr (optarg, "DPU") == optarg) {
  322. vars_to_check = DPU;
  323. strcpy (send_buffer, "DISKSPACE\r\n");
  324. disk_name = strscpy (disk_name, optarg + 3);
  325. }
  326. else
  327. return ERROR;
  328. break;
  329. case 'w': /* warning threshold */
  330. warning_value = strtoul (optarg, NULL, 10);
  331. check_warning_value = TRUE;
  332. break;
  333. case 'c': /* critical threshold */
  334. critical_value = strtoul (optarg, NULL, 10);
  335. check_critical_value = TRUE;
  336. break;
  337. case 't': /* timeout */
  338. socket_timeout = atoi (optarg);
  339. if (socket_timeout <= 0)
  340. return ERROR;
  341. }
  342. }
  343. return OK;
  344. }
  345. void
  346. print_help (void)
  347. {
  348. char *myport;
  349. asprintf (&myport, "%d", PORT);
  350. print_revision (progname, NP_VERSION);
  351. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  352. printf (COPYRIGHT, copyright, email);
  353. printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
  354. printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
  355. printf ("\n\n");
  356. print_usage ();
  357. printf (UT_HELP_VRSN);
  358. printf (UT_EXTRA_OPTS);
  359. printf (UT_HOST_PORT, 'p', myport);
  360. printf (" %s\n", "-w, --warning=INTEGER");
  361. printf (" %s\n", _("Threshold which will result in a warning status"));
  362. printf (" %s\n", "-c, --critical=INTEGER");
  363. printf (" %s\n", _("Threshold which will result in a critical status"));
  364. printf (" %s\n", "-v, --variable=STRING");
  365. printf (" %s\n", _("Variable to check. Valid variables include:"));
  366. printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
  367. printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
  368. printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
  369. printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
  370. printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
  371. printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
  372. printf (" %s\n", _("UPTIME = system uptime in seconds"));
  373. printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  374. printf (UT_VERBOSE);
  375. printf ("\n");
  376. printf ("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be"));
  377. printf ("%s\n", _("running on the remote server."));
  378. printf ("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr"));
  379. printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
  380. printf ("\n");
  381. printf ("%s\n", _("Notes:"));
  382. printf (" %s\n", _("For the available options, the critical threshold value should always be"));
  383. printf (" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
  384. printf (UT_SUPPORT);
  385. }
  386. void
  387. print_usage (void)
  388. {
  389. printf ("%s\n", _("Usage:"));
  390. printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
  391. }