nagios.pm 12 KB

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