gen_nagios_config.pl 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/perl
  2. use utf8;
  3. use open ":encoding(utf8)";
  4. use FindBin '$Bin';
  5. use lib "/opt/Eye/scripts";
  6. use DBI;
  7. use File::Basename;
  8. use File::Find;
  9. use File::stat qw(:FIELDS);
  10. use File::Spec::Functions;
  11. use Sys::Hostname;
  12. use DirHandle;
  13. use Time::localtime;
  14. use Fcntl;
  15. use Tie::File;
  16. use Data::Dumper;
  17. use Net::Ping;
  18. use eyelib::config;
  19. use eyelib::main;
  20. use eyelib::nagios;
  21. use eyelib::snmp;
  22. use eyelib::database;
  23. use Fcntl qw(:flock);
  24. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  25. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  26. my %devices;
  27. my %auths;
  28. my %dependency;
  29. my $nagios_devices = "/etc/snmp/devices.cfg";
  30. my @OU_list = get_records_sql($dbh,"SELECT * FROM OU");
  31. my %ou;
  32. my @cfg_dirs = ();
  33. foreach my $row (@OU_list) {
  34. next if (!$row->{nagios_dir});
  35. if ($row->{nagios_dir}!~/^$config_ref{nagios_dir}/) { $row->{nagios_dir}=$config_ref{nagios_dir}.'/'.$row->{nagios_dir}; }
  36. $row->{nagios_dir}=~s/\/$//;
  37. $ou{$row->{id}}=$row;
  38. push(@cfg_dirs,$row->{nagios_dir});
  39. }
  40. @cfg_dirs = uniq(@cfg_dirs);
  41. my @Model_list = get_records_sql($dbh,"SELECT * FROM device_models");
  42. my %models;
  43. foreach my $row (@Model_list) {
  44. $models{$row->{id}}=$row;
  45. }
  46. #switches & routers only
  47. my @netdev_list=get_records_sql($dbh,'SELECT * FROM devices WHERE deleted=0 and nagios=1 and device_type<=2');
  48. ##################################### Netdevices analyze ################################################
  49. if (scalar(@netdev_list)>0) {
  50. foreach my $router (@netdev_list) {
  51. next if (!$router);
  52. my $ip = $router->{'ip'};
  53. $ip =~s/\/\d+$//g;
  54. my $device_id = 'netdev_'.$router->{'id'};
  55. $devices{$device_id}{ip}=$ip;
  56. setCommunity($router);
  57. $devices{$device_id}{snmp} = $router->{snmp};
  58. $devices{$device_id}{description}=translit($router->{'comment'});
  59. $devices{$device_id}{name} = $router->{'device_name'};
  60. $devices{$device_id}{device_model_id} = $router->{'device_model_id'};
  61. if ($router->{'device_model_id'}) { $devices{$device_id}{device_model} = $models{$router->{'device_model_id'}}; }
  62. $devices{$device_id}{device_id} = $router->{'id'};
  63. $devices{$device_id}{vendor_id} = $router->{'vendor_id'};
  64. $devices{$device_id}{ou_id} = 0;
  65. #1 - switch; 2 - router; 3 - auth
  66. #NOT DEVICE TYPE IN DB!!!
  67. $devices{$device_id}{type}='1';
  68. $devices{$device_id}{ou_id}='7';
  69. if ($router->{'device_type'} eq 2) {
  70. $devices{$device_id}{type}='2';
  71. $devices{$device_id}{ou_id}='10';
  72. }
  73. if ($router->{'user_id'}) {
  74. #get user
  75. my $login = get_record_sql($dbh,"SELECT * FROM User_list WHERE id=".$router->{'user_id'});
  76. if ($login and $login->{ou_id} and $ou{$login->{ou_id}}->{nagios_dir}) { $devices{$device_id}{ou_id} = $login->{ou_id}; }
  77. }
  78. $devices{$device_id}{ou}=$ou{$devices{$device_id}{ou_id}};
  79. if (!$devices{$device_id}{ou}->{nagios_dir}) {
  80. if ($devices{$device_id}{type} eq '1') { $devices{$device_id}{ou}->{nagios_dir}='switches'; }
  81. if ($devices{$device_id}{type} eq '2') { $devices{$device_id}{ou}->{nagios_dir}='routers'; }
  82. }
  83. $devices{$device_id}{user_id}=$router->{'user_id'};
  84. #get uplinks
  85. my $uplink_port = get_record_sql($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");
  86. if ($uplink_port and $uplink_port->{target_port_id}) {
  87. my $parent_uplink = get_record_sql($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{target_port_id}." ORDER BY id DESC");
  88. if ($parent_uplink and $parent_uplink->{device_id}) {
  89. my $uplink_device = get_record_sql($dbh,"SELECT * FROM devices WHERE id=".$parent_uplink->{device_id}." AND nagios=1 AND deleted=0");
  90. if ($uplink_device) {
  91. $devices{$device_id}{parent}='netdev_'.$uplink_device->{'id'};
  92. $devices{$device_id}{parent_name}=$uplink_device->{'device_name'};
  93. }
  94. }
  95. my $uplink = get_record_sql($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{id}." ORDER BY id DESC");
  96. $devices{$device_id}{parent_downlink}=$parent_uplink;
  97. $devices{$device_id}{uplink}=$uplink;
  98. }
  99. #downlinks
  100. my @downlinks = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=".$devices{$device_id}{device_id}." and target_port_id>0 and uplink=0");
  101. foreach my $downlink_port (@downlinks) {
  102. my $downlink = get_record_sql($dbh,"SELECT * FROM device_ports WHERE id=".$downlink_port->{target_port_id});
  103. if ($downlink) {
  104. my $downlink_device = get_record_sql($dbh,"SELECT * FROM devices WHERE id=".$downlink->{device_id});
  105. if ($downlink_device) { $downlink_port->{downlink_name}=$downlink_device->{device_name}; }
  106. }
  107. #id,port,snmp_index
  108. push(@{$devices{$device_id}{downlinks}},$downlink_port);
  109. }
  110. #custom ports
  111. my @custom_ports = get_records_sql($dbh,"SELECT * FROM device_ports WHERE device_id=".$devices{$device_id}{device_id}." and target_port_id=0 and uplink=0 and nagios=1");
  112. foreach my $downlink_port (@custom_ports) {
  113. #id,port,snmp_index,comment
  114. push(@{$devices{$device_id}{downlinks}},$downlink_port);
  115. }
  116. }
  117. }
  118. my @auth_list=get_records_sql($dbh,'SELECT * FROM User_auth WHERE deleted=0 and nagios=1');
  119. ##################################### User auth analyze ################################################
  120. if (scalar(@auth_list)>0) {
  121. foreach my $auth (@auth_list) {
  122. next if (!$auth);
  123. my $ip = $auth->{'ip'};
  124. $ip =~s/\/\d+$//g;
  125. #skip doubles
  126. my $device_id = 'auth_'.$auth->{'id'};
  127. next if ($devices{$device_id});
  128. #skip user device with few ip
  129. my $auth_count = get_count_records($dbh,"User_auth","user_id=".$auth->{'user_id'}." AND deleted=0");
  130. next if ($auth_count>1);
  131. #skip switches and routers
  132. my $auth_device = get_record_sql($dbh,"SELECT * FROM devices WHERE user_id=".$auth->{'user_id'});
  133. next if ($auth_device and $auth_device->{device_type}<=2);
  134. #snmp parameters
  135. setCommunity($auth_device);
  136. $devices{$device_id}{snmp}=$auth_device->{snmp} if ($auth_device->{snmp});
  137. $devices{$device_id}{ip}=$ip;
  138. #get user
  139. my $login = get_record_sql($dbh,"SELECT * FROM User_list WHERE id=".$auth->{'user_id'});
  140. $devices{$device_id}{user_login} = $login->{login};
  141. $devices{$device_id}{user_fio} = $login->{fio};
  142. $devices{$device_id}{ou_id} = 0;
  143. if ($login and $login->{ou_id} and $ou{$login->{ou_id}}->{nagios_dir}) { $devices{$device_id}{ou_id} = $login->{ou_id}; }
  144. $devices{$device_id}{ou}=$ou{$devices{$device_id}{ou_id}};
  145. $devices{$device_id}{device_model_id} = $auth_device->{'device_model_id'};
  146. if ($auth_device->{'device_model_id'}) { $devices{$device_id}{device_model} = $models{$auth_device->{'device_model_id'}}; }
  147. #name
  148. if (!$devices{$device_id}{name} and $auth->{dns_name}) { $devices{$device_id}{name} = $auth->{dns_name}; }
  149. if (!$devices{$device_id}{name}) {
  150. if ($login->{login}) {
  151. $devices{$device_id}{name} = translit($login->{login});
  152. $devices{$device_id}{name}=~s/\(/-/g;
  153. $devices{$device_id}{name}=~s/\)/-/g;
  154. $devices{$device_id}{name}=~s/--/-/g;
  155. } else {
  156. $devices{$device_id}{name} = "auth_id_".$auth->{id};
  157. }
  158. }
  159. $devices{$device_id}{description}=translit($auth->{'comments'}) || $devices{$device_id}{name};
  160. $devices{$device_id}{auth_id} = $auth->{'id'};
  161. $devices{$device_id}{nagios_handler} = $auth->{'nagios_handler'};
  162. $devices{$device_id}{link_check} = $auth->{'link_check'};
  163. $devices{$device_id}{type}='3';
  164. $devices{$device_id}{user_id}=$auth->{'user_id'};
  165. #get uplinks
  166. my $uplink_port = get_record_sql($dbh,"SELECT * FROM connections WHERE auth_id=".$auth->{'id'});
  167. if ($uplink_port and $uplink_port->{port_id}) {
  168. my $uplink = get_record_sql($dbh,"SELECT * FROM device_ports WHERE id=".$uplink_port->{port_id});
  169. if ($uplink and $uplink->{device_id} and $devices{'netdev_'.$uplink->{'device_id'}}) {
  170. $devices{$device_id}{parent}='netdev_'.$uplink->{'device_id'};
  171. $devices{$device_id}{parent_port} = $uplink->{port};
  172. $devices{$device_id}{parent_port_snmp_index} = $uplink->{snmp_index};
  173. $devices{$device_id}{parent_name}=$devices{$devices{$device_id}{parent}}->{'name'};
  174. $devices{$device_id}{parent_snmp}=$devices{$devices{$device_id}{parent}}->{'snmp'};
  175. }
  176. }
  177. }
  178. }
  179. foreach my $dir (@cfg_dirs) {
  180. next if ($dir eq '/');
  181. next if ($dir eq '/etc');
  182. next if ($dir eq $config_ref{nagios_dir});
  183. mkdir $dir unless (-d $dir);
  184. unlink glob "$dir/*.cfg";
  185. }
  186. ##################################### Switches config ################################################
  187. write_to_file($nagios_devices,"#lisf of device for nagios",0);
  188. foreach my $device_id (keys %devices) {
  189. my $device = $devices{$device_id};
  190. next if (!$device->{ip});
  191. if ($device->{parent_name}) { push(@{$dependency{$device->{parent_name}}},$device->{name}); }
  192. print_nagios_cfg($device);
  193. write_to_file($nagios_devices,'$devices{"'.$device->{'ip'}.'"}{"hostname"}="'.$device->{'name'}.'";',1);
  194. }
  195. ####################### Dependency ###########################
  196. open(FH,">",$config_ref{nagios_dir}."/dependency/dep_hosts.cfg");
  197. foreach my $device_name (keys %dependency) {
  198. my @dep_list=@{$dependency{$device_name}};
  199. if (@dep_list and scalar(@dep_list)) {
  200. my $dep_hosts;
  201. foreach my $dep_host (@dep_list) {
  202. next if (!$dep_host);
  203. $dep_hosts = $dep_hosts.",".$dep_host;
  204. }
  205. next if (!$dep_hosts);
  206. $dep_hosts=~s/^,//;
  207. print(FH "define hostdependency {\n");
  208. print(FH " host_name $device_name\n");
  209. print(FH " dependent_host_name $dep_hosts\n");
  210. print(FH " execution_failure_criteria n,u\n");
  211. print(FH " notification_failure_criteria d,u\n");
  212. print(FH " }\n");
  213. }
  214. }
  215. close(FH);
  216. exit