gen_nagios_config.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/perl
  2. use FindBin '$Bin';
  3. use lib "$Bin";
  4. use DBI;
  5. use File::Basename;
  6. use File::Find;
  7. use File::stat qw(:FIELDS);
  8. use File::Spec::Functions;
  9. use Sys::Hostname;
  10. use DirHandle;
  11. use Time::localtime;
  12. use Fcntl;
  13. use Tie::File;
  14. use Data::Dumper;
  15. use Net::Ping;
  16. use Rstat::config;
  17. use Rstat::main;
  18. use Rstat::nagios;
  19. use Rstat::mysql;
  20. my @cfg_dirs = (
  21. '/etc/nagios/switches',
  22. '/etc/nagios/routers',
  23. '/etc/nagios/ap',
  24. '/etc/nagios/voip',
  25. '/etc/nagios/KW-GLOBAL/ipads',
  26. '/etc/nagios/videocam',
  27. '/etc/nagios/KW-GLOBAL/lamps',
  28. '/etc/nagios/printers',
  29. '/etc/nagios/KW-GLOBAL/proj',
  30. '/etc/nagios/KW-GLOBAL/sgvr',
  31. '/etc/nagios/KW-GLOBAL/spav',
  32. '/etc/nagios/KW-GLOBAL/sensors',
  33. '/etc/nagios/ups',
  34. '/etc/nagios/security',
  35. '/etc/nagios/mfi',
  36. '/etc/nagios/any'
  37. );
  38. my %devices;
  39. my %auths;
  40. my %dependency;
  41. my @netdev_list=get_custom_records($dbh,'SELECT * FROM devices WHERE deleted=0 and nagios=1');
  42. ##################################### Netdevices analyze ################################################
  43. if (scalar(@netdev_list)>0) {
  44. foreach my $router (@netdev_list) {
  45. next if (!$router);
  46. my $ip = $router->{'ip'};
  47. $ip =~s/\/\d+$//g;
  48. my $device_id = 'netdev_'.$router->{'id'};
  49. $devices{$device_id}{ip}=$ip;
  50. $devices{$device_id}{community}=$router->{'community'};
  51. $devices{$device_id}{name} = $router->{'device_name'};
  52. $devices{$device_id}{device_model} = $router->{'device_model'};
  53. $devices{$device_id}{device_id} = $router->{'id'};
  54. $devices{$device_id}{snmp_version} = $router->{'snmp_version'};
  55. if ($devices{$device_id}{snmp_version} eq '2') { $devices{$device_id}{snmp_version}='2c'; }
  56. $devices{$device_id}{vendor_id} = $router->{'vendor_id'};
  57. if ($router->{'is_router'}) { $devices{$device_id}{type}='1'; } else { $devices{$device_id}{type}='2'; }
  58. $devices{$device_id}{rw_community}=$router->{'rw_community'};
  59. $devices{$device_id}{fdb_snmp_index}=$router->{'fdb_snmp_index'};
  60. $devices{$device_id}{user_id}=$router->{'user_id'};
  61. #get uplinks
  62. my $uplink_port = get_custom_record($dbh,"SELECT * FROM device_ports WHERE uplink=1 AND device_id=".$devices{$device_id}{device_id}." AND target_port_id>0 ORDER BY port DESC");
  63. if ($uplink_port and $uplink_port->{target_port_id}) {
  64. my $parent_uplink = get_custom_record($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{target_port_id}." ORDER BY id DESC");
  65. if ($parent_uplink and $parent_uplink->{device_id}) {
  66. my $uplink_device = get_custom_record($dbh,"SELECT * FROM devices WHERE id=".$parent_uplink->{device_id}." AND nagios=1 AND deleted=0");
  67. if ($uplink_device) {
  68. $devices{$device_id}{parent}='netdev_'.$uplink_device->{'id'};
  69. $devices{$device_id}{parent_name}=$uplink_device->{'device_name'};
  70. }
  71. }
  72. my $uplink = get_custom_record($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{id}." ORDER BY id DESC");
  73. $devices{$device_id}{parent_downlink}=$parent_uplink;
  74. $devices{$device_id}{uplink}=$uplink;
  75. }
  76. #downlinks
  77. my @downlinks = get_custom_records($dbh,"SELECT * FROM device_ports WHERE device_id=".$devices{$device_id}{device_id}." and target_port_id>0 and uplink=0");
  78. foreach my $downlink_port (@downlinks) {
  79. my $downlink = get_custom_record($dbh,"SELECT * FROM device_ports WHERE id=".$downlink_port->{target_port_id});
  80. if ($downlink) {
  81. my $downlink_device = get_custom_record($dbh,"SELECT * FROM devices WHERE id=".$downlink->{device_id});
  82. if ($downlink_device) { $downlink_port->{downlink_name}=$downlink_device->{device_name}; }
  83. }
  84. #id,port,snmp_index
  85. push(@{$devices{$device_id}{downlinks}},$downlink_port);
  86. }
  87. #custom ports
  88. my @custom_ports = get_custom_records($dbh,"SELECT * FROM device_ports WHERE device_id=".$devices{$device_id}{device_id}." and target_port_id=0 and uplink=0 and nagios=1");
  89. foreach my $downlink_port (@custom_ports) {
  90. #id,port,snmp_index,comment
  91. push(@{$devices{$device_id}{downlinks}},$downlink_port);
  92. }
  93. }
  94. }
  95. my @auth_list=get_custom_records($dbh,'SELECT * FROM User_auth WHERE deleted=0 and nagios=1');
  96. ##################################### User auth analyze ################################################
  97. if (scalar(@auth_list)>0) {
  98. foreach my $auth (@auth_list) {
  99. next if (!$auth);
  100. my $ip = $auth->{'ip'};
  101. $ip =~s/\/\d+$//g;
  102. #skip doubles
  103. my $device_id = 'auth_'.$auth->{'id'};
  104. next if ($devices{$device_id});
  105. $devices{$device_id}{ip}=$ip;
  106. #get user
  107. my $login = get_custom_record($dbh,"SELECT * FROM User_list WHERE id=".$auth->{'user_id'});
  108. $devices{$device_id}{user_login} = $login->{login};
  109. $devices{$device_id}{user_fio} = $login->{fio};
  110. if ($login and $login->{ou_id}) { $devices{$device_id}{ou_id} = $login->{ou_id}; } else { $devices{$device_id}{ou_id} = 0; }
  111. #name
  112. if ($auth->{dns_name}) { $devices{$device_id}{name} = $auth->{dns_name}; }
  113. if (!$devices{$device_id}{name} and $auth->{dhcp_hostname}) { $devices{$device_id}{name} = $auth->{dhcp_hostname}; }
  114. if (!$devices{$device_id}{name}) {
  115. if ($auth->{comments}) {
  116. $devices{$device_id}{name} = translit($auth->{comments});
  117. $devices{$device_id}{name}=~s/\(/-/g;
  118. $devices{$device_id}{name}=~s/\)/-/g;
  119. $devices{$device_id}{name}=~s/--/-/g;
  120. } else {
  121. $devices{$device_id}{name} = $login->{login}."_".$auth->{id};
  122. }
  123. }
  124. $devices{$device_id}{device_model} = $auth->{'host_model'};
  125. $devices{$device_id}{auth_id} = $auth->{'id'};
  126. $devices{$device_id}{nagios_handler} = $auth->{'nagios_handler'};
  127. $devices{$device_id}{link_check} = $auth->{'link_check'};
  128. $devices{$device_id}{type}='3';
  129. $devices{$device_id}{user_id}=$auth->{'user_id'};
  130. #get uplinks
  131. my $uplink_port = get_custom_record($dbh,"SELECT * FROM connections WHERE auth_id=".$auth->{'id'});
  132. if ($uplink_port and $uplink_port->{port_id}) {
  133. my $uplink = get_custom_record($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{port_id});
  134. if ($uplink and $uplink->{device_id} and $devices{'netdev_'.$uplink->{'device_id'}}) {
  135. $devices{$device_id}{parent}='netdev_'.$uplink->{'device_id'};
  136. $devices{$device_id}{parent_port} = $uplink->{port};
  137. $devices{$device_id}{parent_port_snmp_index} = $uplink->{snmp_index};
  138. $devices{$device_id}{parent_name}=$devices{$devices{$device_id}{parent}}->{'name'};
  139. $devices{$device_id}{parent_snmp_version}=$devices{$devices{$device_id}{parent}}->{'snmp_version'};
  140. $devices{$device_id}{parent_community}=$devices{$devices{$device_id}{parent}}->{'community'};
  141. }
  142. }
  143. }
  144. }
  145. foreach my $dir (@cfg_dirs) {
  146. mkdir $dir unless (-d $dir);
  147. unlink glob "$dir/*.cfg";
  148. }
  149. ##################################### Switches config ################################################
  150. foreach my $device_id (keys %devices) {
  151. my $device = $devices{$device_id};
  152. next if (!$device->{ip});
  153. if ($device->{parent_name}) { push(@{$dependency{$device->{parent_name}}},$device->{name}); }
  154. print_nagios_cfg($device);
  155. }
  156. ####################### Dependency ###########################
  157. open(FH,">","/etc/nagios/dependency/dep_hosts.cfg");
  158. foreach my $device_name (keys %dependency) {
  159. my @dep_list=@{$dependency{$device_name}};
  160. if (@dep_list and scalar(@dep_list)) {
  161. my $dep_hosts;
  162. foreach my $dep_host (@dep_list) {
  163. next if (!$dep_host);
  164. $dep_hosts = $dep_hosts.",".$dep_host;
  165. }
  166. next if (!$dep_hosts);
  167. $dep_hosts=~s/^,//;
  168. print(FH "define hostdependency {\n");
  169. print(FH " host_name $device_name\n");
  170. print(FH " dependent_host_name $dep_hosts\n");
  171. print(FH " execution_failure_criteria n,u\n");
  172. print(FH " notification_failure_criteria d,u\n");
  173. print(FH " }\n");
  174. }
  175. }
  176. close(FH);
  177. exit