check_mrtgtraf.c 11 KB

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