check_overcr.c 12 KB

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