nagios.pm 13 KB

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