1
0

check_dhcp_pool.pl 2.7 KB

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