check_dhcp_pool.pl 3.0 KB

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