check_sybase 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/usr/bin/perl -w
  2. # check_sybase
  3. # A nagios plugin that connects to a Sybase database and checks free space.
  4. #
  5. # Copyright 2004 Simon Bellwood, NetMan Network Management and IT Services GmbH
  6. # Portions Copyright 2001 Michael Peppler.
  7. # License: GPL
  8. #
  9. # Bugs and feedback to simon.bellwood@nospam.net-man.at
  10. # Latest version available from:
  11. # http://www.net-man.at/software/check_sybase-LATEST.zip
  12. #
  13. # Revision history:
  14. # 0.1 01-OCT-2004 Initial version.
  15. # 0.2 08-NOV-2004 Initial release.
  16. my $VERSION = "0.2";
  17. use strict;
  18. use DBI;
  19. use Getopt::Long;
  20. use lib "/usr/local/nagios/libexec";
  21. use utils qw(%ERRORS &print_revision &support &usage $TIMEOUT);
  22. my $PROGNAME = "check_sybase";
  23. my $DEFAULT_CHECKTYPE = "FREESPACE";
  24. my $DEFAULT_WARNING = "25";
  25. my $DEFAULT_CRITICAL = "10";
  26. my ($user, $pass, $dbsvr, $dbname, $config, $checktype, $warn, $crit, $timeout,
  27. $help, $version);
  28. my $options_okay = GetOptions(
  29. "U|user=s" => \$user,
  30. "P|pass:s" => \$pass, # ":" means optional
  31. "S|dbsvr=s" => \$dbsvr,
  32. "D|dbname=s" => \$dbname,
  33. "config=s" => \$config,
  34. "checktype=s" => \$checktype,
  35. "w|warning=i" => \$warn,
  36. "c|critical=i" => \$crit,
  37. "t|timeout=i" => \$timeout,
  38. "h|help" => \$help,
  39. "V|version" => \$version
  40. );
  41. if (! $options_okay) # Bad option passed
  42. {
  43. &help;
  44. &nunk("Bad command line option passed!");
  45. }
  46. # Use defaults, if needed
  47. $warn = $warn || $DEFAULT_WARNING;
  48. $crit = $crit || $DEFAULT_CRITICAL;
  49. $checktype = $checktype || $DEFAULT_CHECKTYPE;
  50. $timeout = $timeout || $TIMEOUT;
  51. if ($help)
  52. {
  53. &help;
  54. &nok;
  55. }
  56. if ($version)
  57. {
  58. print_revision($PROGNAME,"\$Revision$VERSION \$");
  59. &nok;
  60. }
  61. if ($config) # Read any of "user", "pass", "dbsvr", "dbname" from config file
  62. {
  63. &read_config;
  64. }
  65. # Some more descriptive syntax checks
  66. my $syntax_error;
  67. $syntax_error .= "No dbsvr given! " unless $dbsvr;
  68. $syntax_error .= "No dbname given! " unless $dbname;
  69. $syntax_error .= "No user given! " unless $user;
  70. $syntax_error .= "Bad checktype given!"
  71. unless $checktype =~ m/^CONNECT|FREESPACE$/;
  72. &nunk($syntax_error) if $syntax_error;
  73. # Just in case of problems, let's not hang Nagios
  74. $SIG{'ALRM'} = sub {
  75. &nunk("Timeout: no response from dbsvr $dbsvr within $timeout seconds");
  76. };
  77. alarm($timeout);
  78. # Decide on what we are checking
  79. if ($checktype eq "CONNECT")
  80. {
  81. &connect;
  82. }
  83. elsif ($checktype eq "FREESPACE")
  84. {
  85. &check_space;
  86. }
  87. my $dbh;
  88. my $is_connected;
  89. sub connect
  90. {
  91. $dbh = DBI->connect("dbi:Sybase:server=$dbsvr;database=$dbname",
  92. $user, $pass)
  93. or &ncrit("Could not connect to '$dbname' on '$dbsvr'");
  94. # Report success for a check of type CONNECT
  95. &nok("Connect okay") if $checktype ne "FREESPACE";
  96. }
  97. sub disconnect
  98. {
  99. $dbh->disconnect if $is_connected;
  100. $is_connected = 0;
  101. }
  102. sub check_space
  103. {
  104. &connect;
  105. # Most of this sub based on Michael Peppler's check-space.pl
  106. $dbh->{syb_do_proc_status} = 1;
  107. my $dbinfo;
  108. # First check space in the database
  109. my $sth = $dbh->prepare("sp_spaceused")
  110. or &nunk("Failed to call sp_spaceused on '$dbsvr'");
  111. $sth->execute
  112. or &nunk("Failed to call sp_spaceused on '$dbsvr'");
  113. do {
  114. while(my $d = $sth->fetch)
  115. {
  116. if($d->[0] =~ /$dbname/)
  117. {
  118. # Grab "database_size"
  119. $d->[1] =~ s/[^\d.]//g;
  120. $dbinfo->{size} = $d->[1];
  121. }
  122. else
  123. {
  124. foreach (@$d)
  125. {
  126. s/\D//g;
  127. }
  128. # Grab "reserved", "data", "index"
  129. $dbinfo->{reserved} = $d->[0] / 1024;
  130. $dbinfo->{data} = $d->[1] / 1024;
  131. $dbinfo->{index} = $d->[2] / 1024;
  132. }
  133. }
  134. } while($sth->{syb_more_results});
  135. # Get the actual device usage from sp_helpdb to get the free log space
  136. $sth = $dbh->prepare("sp_helpdb $dbname")
  137. or &nunk("Failed to call sp_helpdb $dbname on '$dbsvr'");
  138. $sth->execute
  139. or &nunk("Failed to call sp_helpdb $dbname on '$dbsvr'");
  140. do {
  141. while(my $d = $sth->fetch)
  142. {
  143. # Look for "usage" column with value "log only"
  144. if($d->[2] && $d->[2] =~ /log only/)
  145. {
  146. # Grab "size", add it to our log size
  147. $d->[1] =~ s/[^\d\.]//g;
  148. $dbinfo->{log} += $d->[1];
  149. }
  150. # Look for "device fragments" column with "log only"
  151. # followed by a number.
  152. if($d->[0] =~ /log only .* (\d+)/)
  153. {
  154. $dbinfo->{logfree} = $1 / 1024;
  155. }
  156. }
  157. } while($sth->{syb_more_results});
  158. # Subtract the log size from the database size
  159. $dbinfo->{size} -= $dbinfo->{log};
  160. # The "reserved" space is free for use by the table that freed it, so
  161. # it is not truly free space. To be safe, our calculation ignores it.
  162. my $free = ($dbinfo->{size} - $dbinfo->{reserved}) / $dbinfo->{size};
  163. $free = sprintf("%.2f", $free*100);
  164. if ($free < $crit)
  165. {
  166. &ncrit("Free space is $free%! (critical threshold is $crit%)");
  167. }
  168. if ($free < $warn)
  169. {
  170. &nwarn("Free space is $free%! (warning threshold is $warn%)");
  171. }
  172. &nok("Free space within thresholds ($free% free)");
  173. }
  174. sub read_config
  175. {
  176. open (CONFIG, "<$config")
  177. or &nunk("Failed to open config file '$config': $!");
  178. while (<CONFIG>)
  179. {
  180. chomp;
  181. next if m/^#/; # skip comments
  182. next if m/^$/; # skip blanks
  183. # Each case-insensitive argument can be followed by an optional
  184. # colon, then must be followed by whitespace and the value.
  185. # Options in the config file override those given on the
  186. # command line, but don't rely on this!
  187. if (m/USER:?\s+(\S+)/i)
  188. {
  189. $user = $1;
  190. }
  191. elsif (m/PASS:?\s+(\S+)/i)
  192. {
  193. $pass = $1;
  194. }
  195. elsif (m/DBSVR:?\s+(\S+)/i)
  196. {
  197. $dbsvr = $1;
  198. }
  199. elsif (m/DBNAME:?\s+(\S+)/i)
  200. {
  201. $dbname = $1;
  202. }
  203. else
  204. {
  205. &nunk("Invalid line $. in config file '$config'");
  206. }
  207. }
  208. close (CONFIG);
  209. }
  210. sub help
  211. {
  212. print <<_HELP_;
  213. Usage: $PROGNAME OPTIONS
  214. A nagios plugin that connects to a Sybase database and checks free space.
  215. Mandatory arguments to long options are mandatory for short options too.
  216. -U, --user Username to connect to database.
  217. -P, --pass Password to connect to database.
  218. -S, --dbsvr Database server (as in the interfaces file).
  219. -D, --dbname Database name to check.
  220. --config=FILE Config file (see SECURITY below)
  221. --checktype=TYPE Type of check to run (see TYPEs below)
  222. -w, --warning Warning threshold, in percent (default 25)
  223. -c, --critical Critical threshold, in percent (default 10)
  224. -t, --timeout Timeout value, in seconds (default 30)
  225. -h, --help This help message
  226. -V, --version Version information
  227. Examples:
  228. $PROGNAME -U sa -P secret -S bigbox -D orders
  229. $PROGNAME --config=/secure/nagios-sybase.cfg --checktype=CONNECT
  230. TYPEs
  231. There are two types of checks you can run:
  232. --checktype=CONNECT
  233. Checks just the connection to the database.
  234. --checktype=FREESPACE
  235. (Default) Checks both the connection to the database and the free space.
  236. SECURITY - Using a config file
  237. Since a "ps ax" will reveal your database username and password, you can
  238. instead specify them in a config file. Pass the config file with --config.
  239. The format of the file is:
  240. USER value
  241. PASS value
  242. You can also specify a DBSVR and DBNAME in the file. Comments (#) and blank
  243. lines are ignored. Use whitespace to separate argument and value.
  244. _HELP_
  245. }
  246. # Some wrappers..
  247. # Returns code 0, OK
  248. sub nok
  249. {
  250. my $msg = shift;
  251. print "OK: $msg\n" if $msg;
  252. &disconnect;
  253. exit $ERRORS{OK};
  254. }
  255. # Returns code 1, Warning
  256. sub nwarn
  257. {
  258. my $msg = shift;
  259. print "WARNING: $msg\n";
  260. &disconnect;
  261. exit $ERRORS{WARNING};
  262. }
  263. # Returns code 2, Critical
  264. sub ncrit
  265. {
  266. my $msg = shift;
  267. print "CRITICAL: $msg\n";
  268. &disconnect;
  269. exit $ERRORS{CRITICAL};
  270. }
  271. # Returns code 3, Unknown
  272. sub nunk
  273. {
  274. my $msg = shift;
  275. print "ERROR: $msg\n";
  276. &disconnect;
  277. exit $ERRORS{UNKNOWN};
  278. }