check_overcr.c 12 KB

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