check_dhcp_pool.pl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use DBI;
  9. use Time::Local;
  10. use Net::Patricia;
  11. use NetAddr::IP;
  12. use Data::Dumper;
  13. use Rstat::config;
  14. use Rstat::main;
  15. use Rstat::mysql;
  16. use Rstat::net_utils;
  17. use File::Basename;
  18. use File::Path;
  19. my $RET_OK=0;
  20. my $RET_WARNING=1;
  21. my $RET_UNKNOWN=3;
  22. my $RET_CRITICAL=2;
  23. my $MSG_OK="OK";
  24. my $MSG_WARNING="WARN";
  25. my $MSG_CRITICAL="CRIT";
  26. my $warning_limit=$ARGV[1] || 10;
  27. my $crit_limit=$ARGV[2] || 5;
  28. setpriority(0,0,19);
  29. my %dhcp_conf;
  30. my $dhcp_networks = new Net::Patricia;
  31. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 and hotspot=0 ORDER BY ip_int_start');
  32. foreach my $subnet (@subnets) {
  33. next if (!$subnet->{gateway});
  34. my $subnet_name = $subnet->{subnet};
  35. $subnet_name=~s/\/\d+$//g;
  36. $dhcp_networks->add_string($subnet->{subnet},$subnet_name);
  37. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  38. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  39. $dhcp_conf{$subnet_name}->{first_ip_aton}=$subnet->{dhcp_start};
  40. $dhcp_conf{$subnet_name}->{last_ip_aton}=$subnet->{dhcp_stop};
  41. $dhcp_conf{$subnet_name}->{dhcp_pool_size}=$subnet->{dhcp_stop}-$subnet->{dhcp_start};
  42. }
  43. #get userid list
  44. my $sSQL="SELECT id,ip,ip_int,mac,comments,dns_name FROM User_auth where dhcp=1 and deleted=0 and user_id<>$hotspot_user_id and user_id<>$default_user_id ORDER by ip_int";
  45. my @users = get_records_sql($dbh,$sSQL);
  46. foreach my $row (@users) {
  47. next if (!$row);
  48. next if (!$dhcp_networks->match_string($row->{ip}));
  49. next if (!$row->{mac});
  50. next if (!$row->{ip});
  51. my $subnet_name = $dhcp_networks->match_string($row->{ip});
  52. if ($row->{ip_int}~~[$dhcp_conf{$subnet_name}->{first_ip_aton} .. $dhcp_conf{$subnet_name}->{last_ip_aton}]) { $dhcp_conf{$subnet_name}->{dhcp_pool_size}--; }
  53. }
  54. my @warning=();
  55. my @critical=();
  56. foreach my $subnet_name (keys %dhcp_conf) {
  57. if ($dhcp_conf{$subnet_name}->{dhcp_pool_size}>$warning_limit) { next; }
  58. my $free_count = $dhcp_conf{$subnet_name}->{dhcp_pool_size};
  59. if ($free_count <=$warning_limit and $free_count>$crit_limit ) {
  60. push(@warning,"$subnet_name - there are $free_count free addresses left!");
  61. next;
  62. }
  63. if ($free_count <=$crit_limit) {
  64. push(@critical,"$subnet_name - there are $free_count free addresses left!");
  65. next;
  66. }
  67. }
  68. if (scalar(@critical)>0) {
  69. foreach my $row (@critical) { print "$MSG_CRITICAL: $row\n"; }
  70. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  71. exit $RET_CRITICAL;
  72. }
  73. if (scalar(@critical)>0) {
  74. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  75. exit $RET_WARNING;
  76. }
  77. print "$MSG_OK: Dhcp pool OK!\n";
  78. exit $RET_OK;