check_dhcp_pool.pl 2.8 KB

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