check_dhcp_pool.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, description, dns_name
  52. FROM user_auth
  53. WHERE dhcp = 1
  54. AND deleted = 0
  55. AND ou_id NOT IN (?, ?)
  56. ORDER BY ip_int";
  57. my @users = get_records_sql($dbh, $sSQL, $default_hotspot_ou_id, $default_user_ou_id);
  58. foreach my $row (@users) {
  59. next if (!$row);
  60. next if (!$dhcp_networks->match_string($row->{ip}));
  61. next if (!$row->{mac});
  62. next if (!$row->{ip});
  63. my $subnet_name = $dhcp_networks->match_string($row->{ip});
  64. 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}--; }
  65. }
  66. my @warning=();
  67. my @critical=();
  68. foreach my $subnet_name (keys %dhcp_conf) {
  69. if ($dhcp_conf{$subnet_name}->{dhcp_pool_size}>$warning_limit) { next; }
  70. my $free_count = $dhcp_conf{$subnet_name}->{dhcp_pool_size};
  71. if ($free_count <=$warning_limit and $free_count>$crit_limit ) {
  72. push(@warning,"$subnet_name - there are $free_count free addresses left!");
  73. next;
  74. }
  75. if ($free_count <=$crit_limit) {
  76. push(@critical,"$subnet_name - there are $free_count free addresses left!");
  77. next;
  78. }
  79. }
  80. if (scalar(@critical)>0) {
  81. foreach my $row (@critical) { print "$MSG_CRITICAL: $row\n"; }
  82. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  83. exit $RET_CRITICAL;
  84. }
  85. if (scalar(@critical)>0) {
  86. foreach my $row (@warning) { print "$MSG_WARNING: $row\n"; }
  87. exit $RET_WARNING;
  88. }
  89. print "$MSG_OK: Dhcp pool OK!\n";
  90. exit $RET_OK;