check_mrtgtraf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. const char *progname = "check_mrtgtraf";
  54. int process_arguments (int, char **);
  55. int validate_arguments (void);
  56. void print_help (void);
  57. void print_usage (void);
  58. char *log_file = NULL;
  59. int expire_minutes = -1;
  60. int use_average = TRUE;
  61. unsigned long incoming_warning_threshold = 0L;
  62. unsigned long incoming_critical_threshold = 0L;
  63. unsigned long outgoing_warning_threshold = 0L;
  64. unsigned long outgoing_critical_threshold = 0L;
  65. int
  66. main (int argc, char **argv)
  67. {
  68. int result = STATE_OK;
  69. FILE *fp;
  70. int line;
  71. char input_buffer[MAX_INPUT_BUFFER];
  72. char *temp_buffer;
  73. time_t current_time;
  74. char *error_message;
  75. time_t timestamp = 0L;
  76. unsigned long average_incoming_rate = 0L;
  77. unsigned long average_outgoing_rate = 0L;
  78. unsigned long maximum_incoming_rate = 0L;
  79. unsigned long maximum_outgoing_rate = 0L;
  80. unsigned long incoming_rate = 0L;
  81. unsigned long outgoing_rate = 0L;
  82. double adjusted_incoming_rate = 0.0;
  83. double adjusted_outgoing_rate = 0.0;
  84. char incoming_speed_rating[8];
  85. char outgoing_speed_rating[8];
  86. if (process_arguments (argc, argv) != OK)
  87. usage ("Invalid command arguments supplied\n");
  88. /* open the MRTG log file for reading */
  89. fp = fopen (log_file, "r");
  90. if (fp == NULL)
  91. usage ("Unable to open MRTG log file\n");
  92. line = 0;
  93. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  94. line++;
  95. /* skip the first line of the log file */
  96. if (line == 1)
  97. continue;
  98. /* break out of read loop */
  99. /* if we've passed the number of entries we want to read */
  100. if (line > 2)
  101. break;
  102. /* grab the timestamp */
  103. temp_buffer = strtok (input_buffer, " ");
  104. timestamp = strtoul (temp_buffer, NULL, 10);
  105. /* grab the average incoming transfer rate */
  106. temp_buffer = strtok (NULL, " ");
  107. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  108. /* grab the average outgoing transfer rate */
  109. temp_buffer = strtok (NULL, " ");
  110. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  111. /* grab the maximum incoming transfer rate */
  112. temp_buffer = strtok (NULL, " ");
  113. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  114. /* grab the maximum outgoing transfer rate */
  115. temp_buffer = strtok (NULL, " ");
  116. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  117. }
  118. /* close the log file */
  119. fclose (fp);
  120. /* if we couldn't read enough data, return an unknown error */
  121. if (line <= 2)
  122. usage ("Unable to process MRTG log file\n");
  123. /* make sure the MRTG data isn't too old */
  124. time (&current_time);
  125. if (expire_minutes > 0
  126. && (current_time - timestamp) >
  127. (expire_minutes * 60)) terminate (STATE_WARNING,
  128. "MRTG data has expired (%d minutes old)\n",
  129. (int) ((current_time - timestamp) /
  130. 60));
  131. /* else check the incoming/outgoing rates */
  132. if (use_average == TRUE) {
  133. incoming_rate = average_incoming_rate;
  134. outgoing_rate = average_outgoing_rate;
  135. }
  136. else {
  137. incoming_rate = maximum_incoming_rate;
  138. outgoing_rate = maximum_outgoing_rate;
  139. }
  140. /* report incoming traffic in Bytes/sec */
  141. if (incoming_rate < 1024) {
  142. strcpy (incoming_speed_rating, "B/s");
  143. adjusted_incoming_rate = (double) incoming_rate;
  144. }
  145. /* report incoming traffic in KBytes/sec */
  146. else if (incoming_rate < (1024 * 1024)) {
  147. strcpy (incoming_speed_rating, "KB/s");
  148. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  149. }
  150. /* report incoming traffic in MBytes/sec */
  151. else {
  152. strcpy (incoming_speed_rating, "MB/s");
  153. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  154. }
  155. /* report outgoing traffic in Bytes/sec */
  156. if (outgoing_rate < 1024) {
  157. strcpy (outgoing_speed_rating, "B/s");
  158. adjusted_outgoing_rate = (double) outgoing_rate;
  159. }
  160. /* report outgoing traffic in KBytes/sec */
  161. else if (outgoing_rate < (1024 * 1024)) {
  162. strcpy (outgoing_speed_rating, "KB/s");
  163. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  164. }
  165. /* report outgoing traffic in MBytes/sec */
  166. else {
  167. strcpy (outgoing_speed_rating, "MB/s");
  168. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  169. }
  170. if (incoming_rate > incoming_critical_threshold
  171. || outgoing_rate > outgoing_critical_threshold) {
  172. result = STATE_CRITICAL;
  173. asprintf (&error_message, "Traffic CRITICAL %s. In = %0.1f %s, %s. Out = %0.1f %s",
  174. (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
  175. incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
  176. adjusted_outgoing_rate, outgoing_speed_rating);
  177. }
  178. else if (incoming_rate > incoming_warning_threshold
  179. || outgoing_rate > outgoing_warning_threshold) {
  180. result = STATE_WARNING;
  181. asprintf (&error_message, "Traffic WARNING %s. In = %0.1f %s, %s. Out = %0.1f %s",
  182. (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
  183. incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
  184. adjusted_outgoing_rate, outgoing_speed_rating);
  185. }
  186. else if (result == STATE_OK)
  187. printf ("Traffic OK - %s. In = %0.1f %s, %s. Out = %0.1f %s\n",
  188. (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
  189. incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
  190. adjusted_outgoing_rate, outgoing_speed_rating);
  191. else
  192. printf ("UNKNOWN %s\n", error_message);
  193. return result;
  194. }
  195. /* process command-line arguments */
  196. int
  197. process_arguments (int argc, char **argv)
  198. {
  199. int c;
  200. int option_index = 0;
  201. static struct option longopts[] = {
  202. {"logfile", required_argument, 0, 'F'},
  203. {"expires", required_argument, 0, 'e'},
  204. {"aggregation", required_argument, 0, 'a'},
  205. {"variable", required_argument, 0, 'v'},
  206. {"critical", required_argument, 0, 'c'},
  207. {"warning", required_argument, 0, 'w'},
  208. {"verbose", no_argument, 0, 'v'},
  209. {"version", no_argument, 0, 'V'},
  210. {"help", no_argument, 0, 'h'},
  211. {0, 0, 0, 0}
  212. };
  213. if (argc < 2)
  214. return ERROR;
  215. for (c = 1; c < argc; c++) {
  216. if (strcmp ("-to", argv[c]) == 0)
  217. strcpy (argv[c], "-t");
  218. else if (strcmp ("-wt", argv[c]) == 0)
  219. strcpy (argv[c], "-w");
  220. else if (strcmp ("-ct", argv[c]) == 0)
  221. strcpy (argv[c], "-c");
  222. }
  223. while (1) {
  224. c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option_index);
  225. if (c == -1 || c == EOF)
  226. break;
  227. switch (c) {
  228. case 'F': /* input file */
  229. log_file = optarg;
  230. break;
  231. case 'e': /* expiration time */
  232. expire_minutes = atoi (optarg);
  233. break;
  234. case 'a': /* aggregation (AVE or MAX) */
  235. if (!strcmp (optarg, "MAX"))
  236. use_average = FALSE;
  237. else
  238. use_average = TRUE;
  239. break;
  240. case 'c': /* warning threshold */
  241. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  242. &outgoing_critical_threshold);
  243. break;
  244. case 'w': /* critical threshold */
  245. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  246. &outgoing_warning_threshold);
  247. break;
  248. case 'V': /* version */
  249. print_revision (progname, "$Revision$");
  250. exit (STATE_OK);
  251. case 'h': /* help */
  252. print_help ();
  253. exit (STATE_OK);
  254. case '?': /* help */
  255. usage ("Invalid argument\n");
  256. }
  257. }
  258. c = optind;
  259. if (argc > c && log_file == NULL) {
  260. log_file = argv[c++];
  261. }
  262. if (argc > c && expire_minutes == -1) {
  263. expire_minutes = atoi (argv[c++]);
  264. }
  265. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  266. use_average = FALSE;
  267. c++;
  268. }
  269. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  270. use_average = TRUE;
  271. c++;
  272. }
  273. if (argc > c && incoming_warning_threshold == 0) {
  274. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  275. }
  276. if (argc > c && incoming_critical_threshold == 0) {
  277. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  278. }
  279. if (argc > c && outgoing_warning_threshold == 0) {
  280. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  281. }
  282. if (argc > c && outgoing_critical_threshold == 0) {
  283. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  284. }
  285. return validate_arguments ();
  286. }
  287. int
  288. validate_arguments (void)
  289. {
  290. return OK;
  291. }
  292. void
  293. print_help (void)
  294. {
  295. print_revision (progname, "$Revision$");
  296. printf
  297. ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
  298. "This plugin tests the UPS service on the specified host.\n\n");
  299. print_usage ();
  300. printf
  301. ("\nOptions:\n"
  302. " -F, --filename=STRING\n"
  303. " File to read log from\n"
  304. " -e, --expires=INTEGER\n"
  305. " Minutes after which log expires\n"
  306. " -a, --aggregation=(AVG|MAX)\n"
  307. " Test average or maximum"
  308. " -w, --warning\n"
  309. " Warning threshold pair \"<incoming>,<outgoing>\"\n"
  310. " -c, --critical\n"
  311. " Critical threshold pair \"<incoming>,<outgoing>\"\n"
  312. " -h, --help\n"
  313. " Print detailed help screen\n"
  314. " -V, --version\n" " Print version information\n\n");
  315. support ();
  316. }
  317. void
  318. print_usage (void)
  319. {
  320. printf
  321. ("Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n"
  322. " [-e expire_minutes] [-t timeout] [-v]\n"
  323. " %s --help\n"
  324. " %s --version\n", progname, progname, progname);
  325. }