check_dhcp_pool.pl 2.8 KB

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