nagios.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package eyelib::nagios;
  2. #use v5.28;
  3. use utf8;
  4. use open ":encoding(utf8)";
  5. use strict;
  6. use English;
  7. use FindBin '$Bin';
  8. use lib "/opt/Eye/scripts";
  9. use base 'Exporter';
  10. use vars qw(@EXPORT @ISA);
  11. use eyelib::config;
  12. use eyelib::main;
  13. use eyelib::database;
  14. use Time::Local;
  15. use Data::Dumper;
  16. @ISA = qw(Exporter);
  17. @EXPORT = qw(
  18. nagios_send_command
  19. nagios_host_svc_disable
  20. nagios_host_svc_enable
  21. print_nagios_cfg
  22. @cfg_dirs
  23. );
  24. BEGIN
  25. {
  26. #---------------------------------------------------------------------------------
  27. sub nagios_send_command {
  28. my $command = shift;
  29. next if (!$command);
  30. if (!-e $config_ref{nagios_cmd}) { die("Command socket $config_ref{nagios_cmd} not found!"); }
  31. log_info("Send command: $command to $config_ref{nagios_cmd}");
  32. open(FH, ">> $config_ref{nagios_cmd}");
  33. print FH "$command\n";
  34. close(FH);
  35. }
  36. #---------------------------------------------------------------------------------
  37. sub nagios_host_svc_disable {
  38. my $hostname = shift;
  39. my $full = shift || 0;
  40. my $utime = timelocal(localtime());
  41. my $cmd = "[$utime] DISABLE_HOST_SVC_CHECKS;$hostname";
  42. #dont run!!!
  43. #check nagios option work?
  44. #nagios_send_command($cmd);
  45. if ($full) {
  46. $cmd = "[$utime] DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST;$hostname";
  47. nagios_send_command($cmd);
  48. }
  49. $cmd = "[$utime] DISABLE_HOST_SVC_NOTIFICATIONS;$hostname";
  50. nagios_send_command($cmd);
  51. }
  52. #---------------------------------------------------------------------------------
  53. sub nagios_host_svc_enable {
  54. my $hostname = shift;
  55. my $full = shift || 0;
  56. my $utime = timelocal(localtime());
  57. my $cmd = "[$utime] ENABLE_HOST_SVC_CHECKS;$hostname";
  58. nagios_send_command($cmd);
  59. if ($full) {
  60. $cmd = "[$utime] ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST;$hostname";
  61. nagios_send_command($cmd);
  62. }
  63. $cmd = "[$utime] ENABLE_HOST_SVC_NOTIFICATIONS;$hostname";
  64. nagios_send_command($cmd);
  65. }
  66. #---------------------------------------------------------------------------------
  67. sub read_host_template {
  68. my $device = shift;
  69. my $template_file = shift;
  70. my $result;
  71. my @custom_cfg=();
  72. if (-e $template_file) { @custom_cfg = read_file($template_file); } else { return; }
  73. if (@custom_cfg and scalar(@custom_cfg)) {
  74. foreach my $row (@custom_cfg) {
  75. next if (!$row);
  76. $row=~s/\%HOSTNAME\%/$device->{name}/;
  77. $row=~s/\%HOST\%/$device->{name}/;
  78. $row=~s/\%HOSTIP\%/$device->{ip}/;
  79. $row=~s/\%HOST_IP\%/$device->{ip}/;
  80. $row=~s/\%COMMUNITY\%/$device->{community}/ if ($device->{community});
  81. $row=~s/\%RW_COMMUNITY\%/$device->{rw_community}/ if ($device->{rw_community});
  82. $row=~s/\%SNMP_VERSION\%/$device->{snmp_version}/ if ($device->{snmp_version});
  83. $row=~s/\%MODEL\%/$device->{device_model}->{model_name}/ if ($device->{device_model}->{model_name});
  84. push(@{$result->{template}},$row);
  85. if ($row=~/\s+service_description\s+(.*)$/i) { $result->{services}->{$1}=1; }
  86. }
  87. }
  88. return $result;
  89. }
  90. sub print_single_host {
  91. my $device = shift;
  92. my $host_template = 'generic-host';
  93. my $default_service="local-service";
  94. my $ping_enable = $device->{ou}->{nagios_ping};
  95. if ($device->{ou}->{nagios_host_use}) { $host_template=$device->{ou}->{nagios_host_use}; }
  96. if ($device->{ou}->{nagios_default_service}) { $default_service=$device->{ou}->{nagios_default_service}; }
  97. my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
  98. open(FH, "> $cfg_file");
  99. print(FH "define host{\n");
  100. print(FH " use $host_template\n");
  101. print(FH " host_name $device->{name}\n");
  102. print(FH " alias $device->{name}\n");
  103. print(FH " address $device->{ip}\n");
  104. print(FH " _ID $device->{auth_id}\n");
  105. print(FH " _TYPE user\n");
  106. if ($device->{device_model}) {
  107. print(FH " notes $device->{device_model}->{model_name}\n");
  108. }
  109. if ($device->{parent_name}) {
  110. print(FH " parents $device->{parent_name}\n");
  111. }
  112. print(FH " notes_url ".$config_ref{stat_url}."/admin/users/editauth.php?id=".$device->{auth_id}."\n");
  113. print(FH " }\n\n");
  114. if ($ping_enable) {
  115. print(FH "define service{\n");
  116. print(FH " use ping-service\n");
  117. print(FH " host_name $device->{name}\n");
  118. print(FH " service_description ping $device->{name}\n");
  119. print(FH " check_command check_ping_icmp!100.0,20%!500.0,60%\n");
  120. print(FH " }\n");
  121. print(FH "\n");
  122. }
  123. if ($device->{parent_name} and $device->{link_check} and $device->{parent_snmp_version}) {
  124. #port status
  125. print(FH "define service{\n");
  126. print(FH " use $default_service\n");
  127. print(FH " host_name $device->{parent_name}\n");
  128. print(FH " service_description port $device->{parent_port} - $device->{name}\n");
  129. print(FH " check_command check_ifoperstatus!$device->{parent_port_snmp_index}!$device->{parent_community}\n");
  130. print(FH " }\n");
  131. print(FH "\n");
  132. #crc
  133. print(FH "define service{\n");
  134. print(FH " use service-snmp-crc\n");
  135. print(FH " host_name $device->{parent_name}\n");
  136. print(FH " service_description port $device->{parent_port} - $device->{name} CRC Errors\n");
  137. print(FH " check_command check_snmp_switch_crc!$device->{parent_community}!$device->{parent_port_snmp_index}\n");
  138. print(FH " }\n\n");
  139. }
  140. close(FH);
  141. return $cfg_file;
  142. }
  143. #---------------------------------------------------------------------------------
  144. sub print_nagios_cfg {
  145. my $device = shift;
  146. return if (!$device);
  147. my $device_id = $device->{device_id};
  148. my $custom_cfg;
  149. my $device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device->{name}.".cfg";
  150. if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
  151. $device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device_id.".cfg";
  152. if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
  153. my $default_service="local-service";
  154. if (!$device->{ou}->{nagios_dir}) { print Dumper($device); }
  155. my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
  156. if ($custom_cfg->{template}) {
  157. open(FH, "> $cfg_file");
  158. my @custom_cfg = @{$custom_cfg->{template}};
  159. if (@custom_cfg and scalar(@custom_cfg)) {
  160. foreach my $row (@custom_cfg) {
  161. next if (!$row);
  162. print(FH $row."\n");
  163. }
  164. }
  165. close(FH);
  166. return;
  167. }
  168. #switch | router
  169. if (in_array([0,1,2],$device->{type})) {
  170. open(FH, "> $cfg_file");
  171. my $device_template = 'switches';
  172. if ($device->{type} eq 2) { $device_template='routers'; }
  173. print(FH "define host {\n");
  174. print(FH " use $device_template\n");
  175. print(FH " host_name $device->{name}\n");
  176. print(FH " alias $device->{name}\n");
  177. print(FH " address $device->{ip}\n");
  178. print(FH " _ID $device->{device_id}\n");
  179. print(FH " _TYPE device\n");
  180. if ($device->{device_model}) {
  181. print(FH " notes $device->{device_model}->{model_name}\n");
  182. }
  183. if ($device->{parent_name}) {
  184. print(FH " parents $device->{parent_name}\n");
  185. }
  186. print(FH " notes_url ".$config_ref{stat_url}."/admin/devices/editdevice.php?id=$device->{device_id}\n");
  187. print(FH " }\n\n");
  188. #ping
  189. print(FH "define service{\n");
  190. print(FH " use ping-service ; Name of service template to use\n");
  191. print(FH " host_name $device->{name}\n");
  192. print(FH " service_description ping $device->{name}\n");
  193. print(FH " check_command check_ping_icmp!100.0,20%!500.0,60%\n");
  194. print(FH " }\n");
  195. #uptime
  196. if ($device->{snmp_version}) {
  197. print(FH "define service{\n");
  198. print(FH " use $default_service\n");
  199. print(FH " host_name $device->{name}\n");
  200. print(FH " service_description Uptime\n");
  201. print(FH " check_command check_snmp_uptime!$device->{community}!161!$device->{snmp_version}\n");
  202. print(FH " }\n");
  203. print(FH "\n");
  204. #uplink
  205. if (exists $device->{uplink}) {
  206. print(FH "define service{\n");
  207. print(FH " use service-snmp-crc\n");
  208. print(FH " host_name $device->{name}\n");
  209. my $port_description = $device->{parent_name};
  210. my $conn = $device->{uplink};
  211. print(FH " service_description port $conn->{port} - $port_description CRC Errors\n");
  212. print(FH " check_command check_snmp_switch_crc!$device->{community}!$conn->{snmp_index}\n");
  213. print(FH " }\n\n");
  214. }
  215. foreach my $conn (@{$device->{downlinks}}) {
  216. #id,port,snmp_index,comment
  217. print(FH "define service{\n");
  218. print(FH " use $default_service\n");
  219. print(FH " host_name $device->{name}\n");
  220. my $port_description=translit($conn->{comment});
  221. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  222. print(FH " service_description port $conn->{port} - $port_description \n");
  223. print(FH " check_command check_ifoperstatus!$conn->{snmp_index}!$device->{community}\n");
  224. print(FH " }\n\n");
  225. #src
  226. print(FH "define service{\n");
  227. print(FH " use service-snmp-crc\n");
  228. print(FH " host_name $device->{name}\n");
  229. my $port_description=translit($conn->{comment});
  230. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  231. print(FH " service_description port $conn->{port} - $port_description CRC Errors\n");
  232. print(FH " check_command check_snmp_switch_crc!$device->{community}!$conn->{snmp_index}\n");
  233. print(FH " }\n\n");
  234. #band
  235. print(FH "define service{\n");
  236. print(FH " use service-snmp-bandwidth\n");
  237. print(FH " host_name $device->{name}\n");
  238. my $port_description=translit($conn->{comment});
  239. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  240. print(FH " service_description port $conn->{port} - $port_description bandwidth usage\n");
  241. print(FH " check_command check_snmp_bandwidth!$device->{community}!$conn->{snmp_index}\n");
  242. print(FH " }\n\n");
  243. }
  244. }
  245. close FH;
  246. }
  247. #auth record
  248. if ($device->{type} eq 3) {
  249. my $cfg_file = print_single_host($device);
  250. open(FH, ">> $cfg_file");
  251. my $dev_cfg;
  252. if ($device->{device_model} and $device->{device_model}->{nagios_template}) { $dev_cfg = read_host_template($device,$config_ref{nagios_dir}.'/gen_template/'.$device->{device_model}->{nagios_template}); }
  253. if ($dev_cfg and $dev_cfg->{template}) {
  254. my @dev_cfg = @{$dev_cfg->{template}};
  255. if (@dev_cfg and scalar(@dev_cfg)) {
  256. foreach my $row (@dev_cfg) {
  257. next if (!$row);
  258. print(FH $row."\n");
  259. }
  260. }
  261. }
  262. close FH;
  263. }
  264. }
  265. #---------------------------------------------------------------------------------
  266. 1;
  267. }