check_dhcp_pool.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, 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::common;
  23. use eyelib::net_utils;
  24. use File::Basename;
  25. use File::Path;
  26. my $RET_OK=0;
  27. my $RET_WARNING=1;
  28. my $RET_UNKNOWN=3;
  29. my $RET_CRITICAL=2;
  30. my $MSG_OK="OK";
  31. my $MSG_WARNING="WARN";
  32. my $MSG_CRITICAL="CRIT";
  33. my $warning_limit=$ARGV[1] || 10;
  34. my $crit_limit=$ARGV[2] || 5;
  35. setpriority(0,0,19);
  36. my %dhcp_conf;
  37. my $dhcp_networks = new Net::Patricia;
  38. 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');
  39. foreach my $subnet (@subnets) {
  40. next if (!$subnet->{gateway});
  41. my $subnet_name = $subnet->{subnet};
  42. $subnet_name=~s/\/\d+$//g;
  43. $dhcp_networks->add_string($subnet->{subnet},$subnet_name);
  44. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  45. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  46. $dhcp_conf{$subnet_name}->{first_ip_aton}=$subnet->{dhcp_start};
  47. $dhcp_conf{$subnet_name}->{last_ip_aton}=$subnet->{dhcp_stop};
  48. $dhcp_conf{$subnet_name}->{dhcp_pool_size}=$subnet->{dhcp_stop}-$subnet->{dhcp_start};
  49. }
  50. #get userid list
  51. 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";
  52. my @users = get_records_sql($dbh,$sSQL);
  53. foreach my $row (@users) {
  54. next if (!$row);
  55. next if (!$dhcp_networks->match_string($row->{ip}));
  56. next if (!$row->{mac});
  57. next if (!$row->{ip});
  58. my $subnet_name = $dhcp_networks->match_string($row->{ip});
  59. 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}--; }
  60. }
  61. my @warning=();
  62. my @critical=();
  63. foreach my $subnet_name (keys %dhcp_conf) {
  64. if ($dhcp_conf{$subnet_name}->{dhcp_pool_size}>$warning_limit) { next; }
  65. my $free_count = $dhcp_conf{$subnet_name}->{dhcp_pool_size};
  66. if ($free_count <=$warning_limit and $free_count>$crit_limit ) {
  67. push(@warning,"$subnet_name - there are $free_count free addresses left!");
  68. next;
  69. }
  70. if ($free_count <=$crit_limit) {
  71. push(@critical,"$subnet_name - there are $free_count free addresses left!");
  72. next;
  73. }
  74. }
  75. if (scalar(@critical)>0) {
  76. foreach my $row (@critical) { print "$MSG_CRITICAL: $row\n"; }
  77. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  78. exit $RET_CRITICAL;
  79. }
  80. if (scalar(@critical)>0) {
  81. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  82. exit $RET_WARNING;
  83. }
  84. print "$MSG_OK: Dhcp pool OK!\n";
  85. exit $RET_OK;