check_mrtgtraf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /******************************************************************************
  2. *
  3. * CHECK_MRTGTRAF.C
  4. *
  5. * Program: MRTG (Multi-Router Traffic Grapher) traffic plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Command line: CHECK_MRTGTRAF <log_file> <expire_minutes> <AVG|MAX> <iwl> <icl> <owl> <ocl>
  12. *
  13. * Description:
  14. *
  15. * This plugin will check the incoming/outgoing transfer rates of a
  16. * router, switch, etc recorded in an MRTG log. If the newest log
  17. * entry is older than <expire_minutes>, a WARNING status is returned.
  18. * If either the incoming or outgoing rates exceed the <icl> or <ocl>
  19. * thresholds (in Bytes/sec), a CRITICAL status results. If either of
  20. * the rates exceed the <iwl> or <owl> thresholds (in Bytes/sec), a
  21. * WARNING status results.
  22. *
  23. * Notes:
  24. * - MRTG stands for the Multi Router Traffic Grapher. It can be
  25. * downloaded from
  26. * http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html
  27. * - While MRTG can monitor things other than traffic rates, this
  28. * plugin probably won't work with much else without modification.
  29. * - The calculated i/o rates are a little off from what MRTG actually
  30. * reports. I'm not sure why this is right now, but will look into it
  31. * for future enhancements of this plugin.
  32. *
  33. * License Information:
  34. *
  35. * This program is free software; you can redistribute it and/or modify
  36. * it under the terms of the GNU General Public License as published by
  37. * the Free Software Foundation; either version 2 of the License, or
  38. * (at your option) any later version.
  39. *
  40. * This program is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. * GNU General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU General Public License
  46. * along with this program; if not, write to the Free Software
  47. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  48. *
  49. *****************************************************************************/
  50. #include "config.h"
  51. #include "common.h"
  52. #include "utils.h"
  53. #define PROGNAME "check_mrtgtraf"
  54. int process_arguments (int, char **);
  55. int call_getopt (int, char **);
  56. int validate_arguments (void);
  57. void print_help (void);
  58. void print_usage (void);
  59. char *log_file = NULL;
  60. int expire_minutes = -1;
  61. int use_average = TRUE;
  62. unsigned long incoming_warning_threshold = 0L;
  63. unsigned long incoming_critical_threshold = 0L;
  64. unsigned long outgoing_warning_threshold = 0L;
  65. unsigned long outgoing_critical_threshold = 0L;
  66. int
  67. main (int argc, char **argv)
  68. {
  69. int result = STATE_OK;
  70. FILE *fp;
  71. int line;
  72. char input_buffer[MAX_INPUT_BUFFER];
  73. char *temp_buffer;
  74. time_t current_time;
  75. char error_message[MAX_INPUT_BUFFER];
  76. time_t timestamp = 0L;
  77. unsigned long average_incoming_rate = 0L;
  78. unsigned long average_outgoing_rate = 0L;
  79. unsigned long maximum_incoming_rate = 0L;
  80. unsigned long maximum_outgoing_rate = 0L;
  81. unsigned long incoming_rate = 0L;
  82. unsigned long outgoing_rate = 0L;
  83. double adjusted_incoming_rate = 0.0;
  84. double adjusted_outgoing_rate = 0.0;
  85. char incoming_speed_rating[8];
  86. char outgoing_speed_rating[8];
  87. if (process_arguments (argc, argv) != OK)
  88. usage ("Invalid command arguments supplied\n");
  89. /* open the MRTG log file for reading */
  90. fp = fopen (log_file, "r");
  91. if (fp == NULL)
  92. usage ("Unable to open MRTG log file\n");
  93. line = 0;
  94. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  95. line++;
  96. /* skip the first line of the log file */
  97. if (line == 1)
  98. continue;
  99. /* break out of read loop */
  100. /* if we've passed the number of entries we want to read */
  101. if (line > 2)
  102. break;
  103. /* grab the timestamp */
  104. temp_buffer = strtok (input_buffer, " ");
  105. timestamp = strtoul (temp_buffer, NULL, 10);
  106. /* grab the average incoming transfer rate */
  107. temp_buffer = strtok (NULL, " ");
  108. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  109. /* grab the average outgoing transfer rate */
  110. temp_buffer = strtok (NULL, " ");
  111. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  112. /* grab the maximum incoming transfer rate */
  113. temp_buffer = strtok (NULL, " ");
  114. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  115. /* grab the maximum outgoing transfer rate */
  116. temp_buffer = strtok (NULL, " ");
  117. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  118. }
  119. /* close the log file */
  120. fclose (fp);
  121. /* if we couldn't read enough data, return an unknown error */
  122. if (line <= 2)
  123. usage ("Unable to process MRTG log file\n");
  124. /* make sure the MRTG data isn't too old */
  125. time (&current_time);
  126. if (expire_minutes > 0
  127. && (current_time - timestamp) >
  128. (expire_minutes * 60)) terminate (STATE_WARNING,
  129. "MRTG data has expired (%d minutes old)\n",
  130. (int) ((current_time - timestamp) /
  131. 60));
  132. /* else check the incoming/outgoing rates */
  133. if (use_average == TRUE) {
  134. incoming_rate = average_incoming_rate;
  135. outgoing_rate = average_outgoing_rate;
  136. }
  137. else {
  138. incoming_rate = maximum_incoming_rate;
  139. outgoing_rate = maximum_outgoing_rate;
  140. }
  141. /* report incoming traffic in Bytes/sec */
  142. if (incoming_rate < 1024) {
  143. strcpy (incoming_speed_rating, "B/s");
  144. adjusted_incoming_rate = (double) incoming_rate;
  145. }
  146. /* report incoming traffic in KBytes/sec */
  147. else if (incoming_rate < (1024 * 1024)) {
  148. strcpy (incoming_speed_rating, "KB/s");
  149. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  150. }
  151. /* report incoming traffic in MBytes/sec */
  152. else {
  153. strcpy (incoming_speed_rating, "MB/s");
  154. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  155. }
  156. /* report outgoing traffic in Bytes/sec */
  157. if (outgoing_rate < 1024) {
  158. strcpy (outgoing_speed_rating, "B/s");
  159. adjusted_outgoing_rate = (double) outgoing_rate;
  160. }
  161. /* report outgoing traffic in KBytes/sec */
  162. else if (outgoing_rate < (1024 * 1024)) {
  163. strcpy (outgoing_speed_rating, "KB/s");
  164. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  165. }
  166. /* report outgoing traffic in MBytes/sec */
  167. else {
  168. strcpy (outgoing_speed_rating, "MB/s");
  169. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  170. }
  171. if (incoming_rate > incoming_critical_threshold
  172. || outgoing_rate > outgoing_critical_threshold) {
  173. result = STATE_CRITICAL;
  174. sprintf (error_message, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
  175. (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
  176. incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
  177. adjusted_outgoing_rate, outgoing_speed_rating);
  178. }
  179. else if (incoming_rate > incoming_warning_threshold
  180. || outgoing_rate > outgoing_warning_threshold) {
  181. result = STATE_WARNING;
  182. sprintf (error_message, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
  183. (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
  184. incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
  185. adjusted_outgoing_rate, outgoing_speed_rating);
  186. }
  187. if (result == STATE_OK)
  188. printf ("Traffic ok - %s. In = %0.1f %s, %s. Out = %0.1f %s\n",
  189. (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
  190. incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
  191. adjusted_outgoing_rate, outgoing_speed_rating);
  192. else
  193. printf ("%s\n", error_message);
  194. return result;
  195. }
  196. /* process command-line arguments */
  197. int
  198. process_arguments (int argc, char **argv)
  199. {
  200. int c;
  201. if (argc < 2)
  202. return ERROR;
  203. for (c = 1; c < argc; c++) {
  204. if (strcmp ("-to", argv[c]) == 0)
  205. strcpy (argv[c], "-t");
  206. else if (strcmp ("-wt", argv[c]) == 0)
  207. strcpy (argv[c], "-w");
  208. else if (strcmp ("-ct", argv[c]) == 0)
  209. strcpy (argv[c], "-c");
  210. }
  211. c = 0;
  212. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  213. if (is_option (argv[c]))
  214. continue;
  215. if (log_file == NULL) {
  216. log_file = argv[c];
  217. }
  218. else if (expire_minutes == -1) {
  219. expire_minutes = atoi (optarg);
  220. }
  221. else if (strcmp (argv[c], "MAX") == 0) {
  222. use_average = FALSE;
  223. }
  224. else if (strcmp (argv[c], "AVG") == 0) {
  225. use_average = TRUE;
  226. }
  227. else if (incoming_warning_threshold == 0) {
  228. incoming_warning_threshold = strtoul (argv[c], NULL, 10);
  229. }
  230. else if (incoming_critical_threshold == 0) {
  231. incoming_critical_threshold = strtoul (argv[c], NULL, 10);
  232. }
  233. else if (outgoing_warning_threshold == 0) {
  234. outgoing_warning_threshold = strtoul (argv[c], NULL, 10);
  235. }
  236. else if (outgoing_critical_threshold == 0) {
  237. outgoing_critical_threshold = strtoul (argv[c], NULL, 10);
  238. }
  239. }
  240. return validate_arguments ();
  241. }
  242. int
  243. call_getopt (int argc, char **argv)
  244. {
  245. int c, i = 0;
  246. #ifdef HAVE_GETOPT_H
  247. int option_index = 0;
  248. static struct option long_options[] = {
  249. {"logfile", required_argument, 0, 'F'},
  250. {"expires", required_argument, 0, 'e'},
  251. {"aggregation", required_argument, 0, 'a'},
  252. {"variable", required_argument, 0, 'v'},
  253. {"critical", required_argument, 0, 'c'},
  254. {"warning", required_argument, 0, 'w'},
  255. {"verbose", no_argument, 0, 'v'},
  256. {"version", no_argument, 0, 'V'},
  257. {"help", no_argument, 0, 'h'},
  258. {0, 0, 0, 0}
  259. };
  260. #endif
  261. while (1) {
  262. #ifdef HAVE_GETOPT_H
  263. c =
  264. getopt_long (argc, argv, "+hVF:e:a:c:w:", long_options, &option_index);
  265. #else
  266. c = getopt (argc, argv, "+hVF:e:a:c:w:");
  267. #endif
  268. i++;
  269. if (c == -1 || c == EOF || c == 1)
  270. break;
  271. switch (c) {
  272. case 'F':
  273. case 'e':
  274. case 'a':
  275. case 'c':
  276. case 'w':
  277. i++;
  278. }
  279. switch (c) {
  280. case 'F': /* input file */
  281. log_file = optarg;
  282. break;
  283. case 'e': /* expiration time */
  284. expire_minutes = atoi (optarg);
  285. break;
  286. case 'a': /* aggregation (AVE or MAX) */
  287. if (!strcmp (optarg, "MAX"))
  288. use_average = FALSE;
  289. else
  290. use_average = TRUE;
  291. break;
  292. case 'c': /* warning threshold */
  293. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  294. &outgoing_critical_threshold);
  295. break;
  296. case 'w': /* critical threshold */
  297. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  298. &outgoing_warning_threshold);
  299. break;
  300. case 'V': /* version */
  301. print_revision (PROGNAME, "$Revision$");
  302. exit (STATE_OK);
  303. case 'h': /* help */
  304. print_help ();
  305. exit (STATE_OK);
  306. case '?': /* help */
  307. usage ("Invalid argument\n");
  308. }
  309. }
  310. return i;
  311. }
  312. int
  313. validate_arguments (void)
  314. {
  315. return OK;
  316. }
  317. void
  318. print_help (void)
  319. {
  320. print_revision (PROGNAME, "$Revision$");
  321. printf
  322. ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
  323. "This plugin tests the UPS service on the specified host.\n\n");
  324. print_usage ();
  325. printf
  326. ("\nOptions:\n"
  327. " -F, --filename=STRING\n"
  328. " File to read log from\n"
  329. " -e, --expires=INTEGER\n"
  330. " Minutes after which log expires\n"
  331. " -a, --aggregation=(AVG|MAX)\n"
  332. " Test average or maximum"
  333. " -w, --warning\n"
  334. " Warning threshold pair \"<incoming>,<outgoing>\"\n"
  335. " -c, --critical\n"
  336. " Critical threshold pair \"<incoming>,<outgoing>\"\n"
  337. " -h, --help\n"
  338. " Print detailed help screen\n"
  339. " -V, --version\n" " Print version information\n\n");
  340. support ();
  341. }
  342. void
  343. print_usage (void)
  344. {
  345. printf
  346. ("Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n"
  347. " [-e expire_minutes] [-t timeout] [-v]\n"
  348. " %s --help\n"
  349. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  350. }