gen_nagios_config.pl 10 KB

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