check_mrtgtraf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "common.h"
  15. #include "utils.h"
  16. const char *progname = "check_mrtgtraf";
  17. const char *revision = "$Revision$";
  18. const char *copyright = "1999-2003";
  19. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  20. int process_arguments (int, char **);
  21. int validate_arguments (void);
  22. void print_help(void);
  23. void print_usage(void);
  24. char *log_file = NULL;
  25. int expire_minutes = -1;
  26. int use_average = TRUE;
  27. unsigned long incoming_warning_threshold = 0L;
  28. unsigned long incoming_critical_threshold = 0L;
  29. unsigned long outgoing_warning_threshold = 0L;
  30. unsigned long outgoing_critical_threshold = 0L;
  31. int
  32. main (int argc, char **argv)
  33. {
  34. int result = STATE_OK;
  35. FILE *fp;
  36. int line;
  37. char input_buffer[MAX_INPUT_BUFFER];
  38. char *temp_buffer;
  39. time_t current_time;
  40. char *error_message;
  41. time_t timestamp = 0L;
  42. unsigned long average_incoming_rate = 0L;
  43. unsigned long average_outgoing_rate = 0L;
  44. unsigned long maximum_incoming_rate = 0L;
  45. unsigned long maximum_outgoing_rate = 0L;
  46. unsigned long incoming_rate = 0L;
  47. unsigned long outgoing_rate = 0L;
  48. double adjusted_incoming_rate = 0.0;
  49. double adjusted_outgoing_rate = 0.0;
  50. char incoming_speed_rating[8];
  51. char outgoing_speed_rating[8];
  52. if (process_arguments (argc, argv) != OK)
  53. usage (_("Invalid command arguments supplied\n"));
  54. /* open the MRTG log file for reading */
  55. fp = fopen (log_file, "r");
  56. if (fp == NULL)
  57. usage (_("Unable to open MRTG log file\n"));
  58. line = 0;
  59. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  60. line++;
  61. /* skip the first line of the log file */
  62. if (line == 1)
  63. continue;
  64. /* break out of read loop */
  65. /* if we've passed the number of entries we want to read */
  66. if (line > 2)
  67. break;
  68. /* grab the timestamp */
  69. temp_buffer = strtok (input_buffer, " ");
  70. timestamp = strtoul (temp_buffer, NULL, 10);
  71. /* grab the average incoming transfer rate */
  72. temp_buffer = strtok (NULL, " ");
  73. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  74. /* grab the average outgoing transfer rate */
  75. temp_buffer = strtok (NULL, " ");
  76. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  77. /* grab the maximum incoming transfer rate */
  78. temp_buffer = strtok (NULL, " ");
  79. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  80. /* grab the maximum outgoing transfer rate */
  81. temp_buffer = strtok (NULL, " ");
  82. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  83. }
  84. /* close the log file */
  85. fclose (fp);
  86. /* if we couldn't read enough data, return an unknown error */
  87. if (line <= 2)
  88. usage (_("Unable to process MRTG log file\n"));
  89. /* make sure the MRTG data isn't too old */
  90. time (&current_time);
  91. if ((expire_minutes > 0) &&
  92. (current_time - timestamp) > (expire_minutes * 60))
  93. die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
  94. (int) ((current_time - timestamp) / 60));
  95. /* else check the incoming/outgoing rates */
  96. if (use_average == TRUE) {
  97. incoming_rate = average_incoming_rate;
  98. outgoing_rate = average_outgoing_rate;
  99. }
  100. else {
  101. incoming_rate = maximum_incoming_rate;
  102. outgoing_rate = maximum_outgoing_rate;
  103. }
  104. /* report incoming traffic in Bytes/sec */
  105. if (incoming_rate < 1024) {
  106. strcpy (incoming_speed_rating, "B/s");
  107. adjusted_incoming_rate = (double) incoming_rate;
  108. }
  109. /* report incoming traffic in KBytes/sec */
  110. else if (incoming_rate < (1024 * 1024)) {
  111. strcpy (incoming_speed_rating, "KB/s");
  112. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  113. }
  114. /* report incoming traffic in MBytes/sec */
  115. else {
  116. strcpy (incoming_speed_rating, "MB/s");
  117. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  118. }
  119. /* report outgoing traffic in Bytes/sec */
  120. if (outgoing_rate < 1024) {
  121. strcpy (outgoing_speed_rating, "B/s");
  122. adjusted_outgoing_rate = (double) outgoing_rate;
  123. }
  124. /* report outgoing traffic in KBytes/sec */
  125. else if (outgoing_rate < (1024 * 1024)) {
  126. strcpy (outgoing_speed_rating, "KB/s");
  127. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  128. }
  129. /* report outgoing traffic in MBytes/sec */
  130. else {
  131. strcpy (outgoing_speed_rating, "MB/s");
  132. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  133. }
  134. if (incoming_rate > incoming_critical_threshold
  135. || outgoing_rate > outgoing_critical_threshold) {
  136. result = STATE_CRITICAL;
  137. }
  138. else if (incoming_rate > incoming_warning_threshold
  139. || outgoing_rate > outgoing_warning_threshold) {
  140. result = STATE_WARNING;
  141. }
  142. asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
  143. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  144. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  145. adjusted_outgoing_rate, outgoing_speed_rating,
  146. perfdata("in", (long) adjusted_incoming_rate, incoming_speed_rating,
  147. (int) incoming_warning_threshold, (long) incoming_warning_threshold,
  148. (int) incoming_critical_threshold, (long) incoming_critical_threshold,
  149. TRUE, 0, TRUE, (long) incoming_speed_rating),
  150. perfdata("in", (long) adjusted_outgoing_rate, outgoing_speed_rating,
  151. (int) outgoing_warning_threshold, (long) outgoing_warning_threshold,
  152. (int) outgoing_critical_threshold, (long) outgoing_critical_threshold,
  153. TRUE, 0, TRUE, (long) outgoing_speed_rating));
  154. printf (_("Traffic %s - %s\n"), state_text(result), error_message);
  155. return result;
  156. }
  157. /* process command-line arguments */
  158. int
  159. process_arguments (int argc, char **argv)
  160. {
  161. int c;
  162. int option = 0;
  163. static struct option longopts[] = {
  164. {"logfile", required_argument, 0, 'F'},
  165. {"expires", required_argument, 0, 'e'},
  166. {"aggregation", required_argument, 0, 'a'},
  167. {"variable", required_argument, 0, 'v'},
  168. {"critical", required_argument, 0, 'c'},
  169. {"warning", required_argument, 0, 'w'},
  170. {"verbose", no_argument, 0, 'v'},
  171. {"version", no_argument, 0, 'V'},
  172. {"help", no_argument, 0, 'h'},
  173. {0, 0, 0, 0}
  174. };
  175. if (argc < 2)
  176. return ERROR;
  177. for (c = 1; c < argc; c++) {
  178. if (strcmp ("-to", argv[c]) == 0)
  179. strcpy (argv[c], "-t");
  180. else if (strcmp ("-wt", argv[c]) == 0)
  181. strcpy (argv[c], "-w");
  182. else if (strcmp ("-ct", argv[c]) == 0)
  183. strcpy (argv[c], "-c");
  184. }
  185. while (1) {
  186. c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
  187. if (c == -1 || c == EOF)
  188. break;
  189. switch (c) {
  190. case 'F': /* input file */
  191. log_file = optarg;
  192. break;
  193. case 'e': /* expiration time */
  194. expire_minutes = atoi (optarg);
  195. break;
  196. case 'a': /* aggregation (AVE or MAX) */
  197. if (!strcmp (optarg, "MAX"))
  198. use_average = FALSE;
  199. else
  200. use_average = TRUE;
  201. break;
  202. case 'c': /* warning threshold */
  203. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  204. &outgoing_critical_threshold);
  205. break;
  206. case 'w': /* critical threshold */
  207. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  208. &outgoing_warning_threshold);
  209. break;
  210. case 'V': /* version */
  211. print_revision (progname, revision);
  212. exit (STATE_OK);
  213. case 'h': /* help */
  214. print_help ();
  215. exit (STATE_OK);
  216. case '?': /* help */
  217. usage (_("Invalid argument\n"));
  218. }
  219. }
  220. c = optind;
  221. if (argc > c && log_file == NULL) {
  222. log_file = argv[c++];
  223. }
  224. if (argc > c && expire_minutes == -1) {
  225. expire_minutes = atoi (argv[c++]);
  226. }
  227. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  228. use_average = FALSE;
  229. c++;
  230. }
  231. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  232. use_average = TRUE;
  233. c++;
  234. }
  235. if (argc > c && incoming_warning_threshold == 0) {
  236. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  237. }
  238. if (argc > c && incoming_critical_threshold == 0) {
  239. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  240. }
  241. if (argc > c && outgoing_warning_threshold == 0) {
  242. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  243. }
  244. if (argc > c && outgoing_critical_threshold == 0) {
  245. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  246. }
  247. return validate_arguments ();
  248. }
  249. int
  250. validate_arguments (void)
  251. {
  252. return OK;
  253. }
  254. void
  255. print_help (void)
  256. {
  257. print_revision (progname, revision);
  258. printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
  259. printf (_(COPYRIGHT), copyright, email);
  260. print_usage ();
  261. printf (_(UT_HELP_VRSN));
  262. printf (_("\
  263. -F, --filename=STRING\n\
  264. File to read log from\n\
  265. -e, --expires=INTEGER\n\
  266. Minutes after which log expires\n\
  267. -a, --aggregation=(AVG|MAX)\n\
  268. Test average or maximum\n\
  269. -w, --warning\n\
  270. Warning threshold pair \"<incoming>,<outgoing>\"\n\
  271. -c, --critical\n\
  272. Critical threshold pair \"<incoming>,<outgoing>\"\n"));
  273. printf (_("\n\
  274. This plugin will check the incoming/outgoing transfer rates of a router,\n\
  275. switch, etc recorded in an MRTG log. If the newest log entry is older\n\
  276. than <expire_minutes>, a WARNING status is returned. If either the\n\
  277. incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in\n\
  278. Bytes/sec), a CRITICAL status results. If either of the rates exceed\n\
  279. the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results.\n\n"));
  280. printf (_("Notes:\n\
  281. - MRTG stands for Multi Router Traffic Grapher. It can be downloaded from\n\
  282. http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html\n\
  283. - While MRTG can monitor things other than traffic rates, this\n\
  284. plugin probably won't work with much else without modification.\n\
  285. - The calculated i/o rates are a little off from what MRTG actually\n\
  286. reports. I'm not sure why this is right now, but will look into it\n\
  287. for future enhancements of this plugin.\n"));
  288. printf (_(UT_SUPPORT));
  289. }
  290. void
  291. print_usage (void)
  292. {
  293. printf (_("\
  294. Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n\
  295. [-e expire_minutes] [-t timeout] [-v]\n"), progname);
  296. printf (_(UT_HLP_VRS), progname, progname);
  297. }