PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/mrtgext.pl

https://github.com/Elbandi/nagios-plugins
Perl | 291 lines | 188 code | 40 blank | 63 comment | 19 complexity | 6a25a29ace258fd0da098bf36d7312c2 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/perl -w
  2. #
  3. # mrtgext.pl v0.3
  4. # (c)2000 Cliff Woolley, Washington and Lee University
  5. # jwoolley@wlu.edu
  6. #
  7. # A UNIX counterpart to Jim Drews' MRTG Extension for netware servers
  8. # Mimics output of mrtgext.nlm using output of various standard UNIX
  9. # programs (df, uptime, and uname)
  10. #
  11. # Dependencies: I make some assumptions about the output format of
  12. # your df and uptime commands. If you have nonstandard outputs for
  13. # any of these, either pick a different command that gives more
  14. # standard output or modify the script below. Example: use /usr/bin/bdf
  15. # on HP-UX instead of /usr/bin/df, because bdf follows the output format
  16. # I expect while df does not. This was written on Linux and tested on
  17. # HP-UX 10.20 (with changes to the subroutines at the bottom of the
  18. # program to reflect HP's command parameters); similar tweaking could
  19. # well be required to port this to other platforms. If you get it
  20. # working on your platform, please send me any changes you had to
  21. # make so I can try to incorporate them.
  22. #
  23. #
  24. # Following is what I expect the programs' outputs to look like:
  25. #
  26. # ======= df ========
  27. # Filesystem 1k-blocks Used Available Use% Mounted on
  28. # /dev/sda1 1014696 352708 609612 37% /
  29. # /dev/sda2 2262544 586712 1559048 27% /apps
  30. # /dev/sda3 4062912 566544 3286604 15% /share
  31. # /dev/sr0 651758 651758 0 100% /cdrom
  32. # ===================
  33. #
  34. # ===== uptime ======
  35. # 3:17pm up 15 days, 4:40, 5 users, load average: 0.12, 0.26, 0.33
  36. # ===================
  37. #
  38. ###############################################################
  39. # Configuration section
  40. ###############################################################
  41. $dfcmd = "/bin/df 2>/dev/null";
  42. $uptimecmd = "/usr/bin/uptime";
  43. %customcmds = ( "PROCS" => "numprocesses",
  44. "ZOMBIES" => "numzombies",
  45. "MEMFREE" => "memfree",
  46. "SWAPUSED" => "swapused",
  47. "TCPCONNS" => "tcpconns",
  48. "CLIENTS" => "ipclients" );
  49. # These are functions that you can
  50. # define and customize for your system.
  51. # You probably need to change the provided
  52. # subroutines to work on your system (if
  53. # not Linux).
  54. $rootfsnickname = "root"; # this is necessary as a kludge to
  55. # better match the netware behavior.
  56. # if you already have a _filesystem_
  57. # mounted as /root, then you'll need
  58. # to change this to something else
  59. $DEBUG = 0;
  60. $recvtimeout = 30;
  61. ###############################################################
  62. # Program section
  63. ###############################################################
  64. require 5.004;
  65. use Sys::Hostname;
  66. $DEBUG = $ARGV[0] unless ($DEBUG);
  67. $SIG{'ALRM'} = sub { exit 1; };
  68. # some things never change
  69. $hostname = hostname;
  70. if ( $DEBUG ) {
  71. $| = 1;
  72. print scalar localtime,": mrtgext.pl started\n";
  73. }
  74. # timeout period
  75. alarm($recvtimeout);
  76. my $items = <STDIN>;
  77. alarm(0);
  78. $items =~ s/[\r\n]//g;
  79. ( $DEBUG ) && print scalar localtime,": request: \"$items\"\n";
  80. my @items = split (/\s+/,"$items");
  81. ( $DEBUG ) && print scalar localtime,": ",scalar @items," item(s) to process\n";
  82. my $uptime = `$uptimecmd`;
  83. my @df = grep {/^\//} `$dfcmd`;
  84. my $processed = 1;
  85. foreach $_ (@items) {
  86. ( $DEBUG ) && print scalar localtime,": processing item #$processed: \"$_\"\n";
  87. $_ = uc; #convert $_ to upper case
  88. if ( /^UTIL1$/ ) {
  89. $uptime =~ /load average: ([^,]+),/;
  90. print $1 * 100,"\n";
  91. }
  92. elsif ( /^UTIL5$/ ) {
  93. $uptime =~ /load average: [^,]+, ([^,]+)/;
  94. print $1 * 100,"\n";
  95. }
  96. elsif ( /^UTIL15$/ ) {
  97. $uptime =~ /load average: [^,]+, [^,]+, ([^,]+)/;
  98. print $1 * 100,"\n";
  99. }
  100. elsif ( /^CONNECT$/ ) {
  101. $uptime =~ /(\d+) users?,/;
  102. print "$1\n";
  103. }
  104. elsif ( /^NAME$/ ) {
  105. print "$hostname\n";
  106. }
  107. elsif ( /^UPTIME$/ ) {
  108. $uptime =~ /up (.*),\s+\d+\s+users?,/;
  109. print "$1\n";
  110. }
  111. elsif ( /^VOLUMES$/ ) {
  112. foreach $dfline (@df) {
  113. my $volname = (split(/\s+/, "$dfline"))[5];
  114. $volname =~ s/^\/$/$rootfsnickname/;
  115. $volname =~ s/^\///;
  116. $volname =~ s/\//_/g;
  117. print "$volname\n";
  118. }
  119. }
  120. elsif ( /^VF(\w*)$/ ) {
  121. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  122. foreach $dfline (@df) {
  123. my @dfline = split(/\s+/, "$dfline");
  124. if ($dfline[5] =~ /^\/?$volname$/i ) {
  125. print (($dfline[1]-$dfline[2]) * 1024,"\n");
  126. goto done;
  127. }
  128. }
  129. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  130. print "-1\n";
  131. }
  132. elsif ( /^VU(\w*)$/ ) {
  133. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  134. foreach $dfline (@df) {
  135. my @dfline = split(/\s+/, "$dfline");
  136. if ($dfline[5] =~ /^\/?$volname$/i ) {
  137. print ($dfline[2] * 1024,"\n");
  138. goto done;
  139. }
  140. }
  141. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  142. print "-1\n";
  143. }
  144. elsif ( /^VS(\w*)$/ ) {
  145. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  146. foreach $dfline (@df) {
  147. my @dfline = split(/\s+/, "$dfline");
  148. if ($dfline[5] =~ /^\/?$volname$/i ) {
  149. print ($dfline[1] * 1024,"\n");
  150. goto done;
  151. }
  152. }
  153. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  154. print "-1\n";
  155. }
  156. elsif ( /^VKF(\w*)$/ ) {
  157. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  158. foreach $dfline (@df) {
  159. my @dfline = split(/\s+/, "$dfline");
  160. if ($dfline[5] =~ /^\/?$volname$/i ) {
  161. print (($dfline[1]-$dfline[2]),"\n");
  162. goto done;
  163. }
  164. }
  165. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  166. print "-1\n";
  167. }
  168. elsif ( /^VKU(\w*)$/ ) {
  169. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  170. foreach $dfline (@df) {
  171. my @dfline = split(/\s+/, "$dfline");
  172. if ($dfline[5] =~ /^\/?$volname$/i ) {
  173. print ($dfline[2],"\n");
  174. goto done;
  175. }
  176. }
  177. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  178. print "-1\n";
  179. }
  180. elsif ( /^VKS(\w*)$/ ) {
  181. my $volname = ("$1" eq uc("$rootfsnickname")) ? "/" : "$1";
  182. foreach $dfline (@df) {
  183. my @dfline = split(/\s+/, "$dfline");
  184. if ($dfline[5] =~ /^\/?$volname$/i ) {
  185. print ($dfline[1],"\n");
  186. goto done;
  187. }
  188. }
  189. ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
  190. print "-1\n";
  191. }
  192. elsif ( /^ZERO$/ ) {
  193. print "0\n";
  194. }
  195. elsif (exists( $customcmds{"$_"} )) {
  196. my $cmdsub = "$customcmds{$_}";
  197. print &$cmdsub."\n";
  198. }
  199. else {
  200. print "-1\n";
  201. }
  202. done: $processed++;
  203. }
  204. ( $DEBUG ) && print scalar localtime,": done.\n";
  205. ###############################################################
  206. # CUSTOMIZED PROCEDURES
  207. ###############################################################
  208. sub numprocesses {
  209. my $num = `/bin/ps -eaf | /usr/bin/tail -n +2 | /usr/bin/wc -l`;
  210. chomp ($num);
  211. $num =~ s/\s+//g;
  212. $num;
  213. }
  214. sub numzombies {
  215. my $num = `/bin/ps -afx | /usr/bin/awk '{print \$3}' | /usr/bin/grep Z | /usr/bin/tail -n +2 | /usr/bin/wc -l`;
  216. chomp ($num);
  217. $num =~ s/\s+//g;
  218. $num;
  219. }
  220. sub tcpconns {
  221. my $num = `/bin/netstat -nt | /usr/bin/tail -n +3 | /usr/bin/wc -l`;
  222. chomp ($num);
  223. $num =~ s/\s+//g;
  224. $num;
  225. }
  226. sub ipclients {
  227. my $num = `/bin/netstat -nt | /usr/bin/tail -n +3 | /usr/bin/awk '{print \$5}' | /bin/cut -d : -f 1 | /usr/bin/sort -nu | /usr/bin/wc -l`;
  228. chomp ($num);
  229. $num =~ s/\s+//g;
  230. $num;
  231. }
  232. sub memfree {
  233. open( FP, "/proc/meminfo" );
  234. my @meminfo = <FP>;
  235. close(FP);
  236. # total: used: free: shared: buffers: cached:
  237. # Mem: 994615296 592801792 401813504 91193344 423313408 93118464
  238. # Swap: 204791808 0 204791808
  239. my ($total,$free,$buffers,$cache) = (split(/ +/,$meminfo[1]))[1,3,5,6];
  240. int(($free+$buffers+$cache)/$total*100);
  241. }
  242. sub swapused {
  243. open( FP, "/proc/meminfo" );
  244. my @meminfo = <FP>;
  245. close(FP);
  246. # total: used: free: shared: buffers: cached:
  247. # Mem: 994615296 592424960 402190336 89821184 423313408 93077504
  248. # Swap: 204791808 0 204791808
  249. my ($total,$used) = (split(/ +/,$meminfo[2]))[1,2];
  250. int($used/$total*100);
  251. }