nagios.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 eyelib::snmp;
  15. use Time::Local;
  16. use Data::Dumper;
  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. if (!exists $device->{parent_snmp} and !$device->{snmp}) { setCommunity($device); }
  73. if (exists $device->{parent_snmp} and !$device->{snmp}) { $device->{snmp} = $device->{parent_snmp}; }
  74. my @custom_cfg=();
  75. if (-e $template_file) { @custom_cfg = read_file($template_file); } else { return; }
  76. if (@custom_cfg and scalar(@custom_cfg)) {
  77. foreach my $row (@custom_cfg) {
  78. next if (!$row);
  79. $row=~s/\%HOSTNAME\%/$device->{name}/;
  80. $row=~s/\%HOST\%/$device->{name}/;
  81. $row=~s/\%HOSTIP\%/$device->{ip}/;
  82. $row=~s/\%HOST_IP\%/$device->{ip}/;
  83. $row=~s/\%COMMUNITY\%/$device->{snmp}->{'ro-community'}/ if ($device->{snmp}->{'ro-community'});
  84. $row=~s/\%RW_COMMUNITY\%/$device->{snmp}->{'rw-community'}/ if ($device->{snmp}->{'rw-community'});
  85. $row=~s/\%SNMP_VERSION\%/$device->{snmp}->{'version'}/ if ($device->{snmp}->{'version'});
  86. $row=~s/\%SNMP_V3_AUTH\%/$device->{snmp}->{'auth-proto'}/ if ($device->{snmp}->{'auth-proto'});
  87. $row=~s/\%SNMP_V3_PRIV\%/$device->{snmp}->{'priv-proto'}/ if ($device->{snmp}->{'priv-proto'});
  88. $row=~s/\%SNMP_V3_RO_PASSWORD\%/$device->{snmp}->{'ro-password'}/ if ($device->{snmp}->{'ro-password'});
  89. $row=~s/\%SNMP_V3_RW_PASSWORD\%/$device->{snmp}->{'rw-password'}/ if ($device->{snmp}->{'rw-password'});
  90. $row=~s/\%MODEL\%/$device->{device_model}->{model_name}/ if ($device->{device_model}->{model_name});
  91. push(@{$result->{template}},$row);
  92. if ($row=~/\s+service_description\s+(.*)$/i) { $result->{services}->{$1}=1; }
  93. }
  94. }
  95. return $result;
  96. }
  97. sub print_single_host {
  98. my $device = shift;
  99. my $host_template = 'generic-host';
  100. my $default_service="local-service";
  101. my $snmp_string = $device->{parent_snmp}->{'ro-community'};
  102. if ($device->{parent_snmp}->{version} > 2) {
  103. $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'});
  104. }
  105. my $ping_enable = $device->{ou}->{nagios_ping};
  106. if ($device->{ou}->{nagios_host_use}) { $host_template=$device->{ou}->{nagios_host_use}; }
  107. if ($device->{ou}->{nagios_default_service}) { $default_service=$device->{ou}->{nagios_default_service}; }
  108. my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
  109. open(FH, "> $cfg_file");
  110. print(FH "define host{\n");
  111. print(FH " use $host_template\n");
  112. print(FH " host_name $device->{name}\n");
  113. print(FH " alias $device->{name}\n");
  114. print(FH " address $device->{ip}\n");
  115. print(FH " _ID $device->{auth_id}\n");
  116. print(FH " _TYPE user\n");
  117. if ($device->{device_model}) {
  118. print(FH " notes $device->{device_model}->{model_name}\n");
  119. }
  120. if ($device->{parent_name}) {
  121. print(FH " parents $device->{parent_name}\n");
  122. }
  123. print(FH " notes_url ".$config_ref{stat_url}."/admin/users/editauth.php?id=".$device->{auth_id}."\n");
  124. print(FH " }\n\n");
  125. if ($ping_enable) {
  126. print(FH "define service{\n");
  127. print(FH " use ping-service\n");
  128. print(FH " host_name $device->{name}\n");
  129. print(FH " service_description ping $device->{name}\n");
  130. print(FH " check_command check_ping!100.0,20%!500.0,60%\n");
  131. print(FH " }\n");
  132. print(FH "\n");
  133. }
  134. if ($device->{parent_name} and $device->{link_check} and $device->{parent_snmp}->{version}) {
  135. #port status
  136. print(FH "define service{\n");
  137. print(FH " use $default_service\n");
  138. print(FH " host_name $device->{parent_name}\n");
  139. print(FH " service_description port $device->{parent_port} - $device->{name}\n");
  140. if ($device->{parent_snmp}->{version}<3) {
  141. print(FH " check_command check_ifoperstatus!$device->{parent_port_snmp_index}!$device->{parent_snmp}->{'ro-community'}\n");
  142. } else {
  143. 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");
  144. }
  145. print(FH " }\n");
  146. print(FH "\n");
  147. #crc
  148. print(FH "define service{\n");
  149. print(FH " use service-snmp-crc\n");
  150. print(FH " host_name $device->{parent_name}\n");
  151. print(FH " service_description port $device->{parent_port} - $device->{name} CRC Errors\n");
  152. print(FH " check_command check_snmp_switch_crc!$snmp_string!$device->{parent_port_snmp_index}\n");
  153. print(FH " }\n\n");
  154. }
  155. close(FH);
  156. return $cfg_file;
  157. }
  158. #---------------------------------------------------------------------------------
  159. sub print_nagios_cfg {
  160. my $device = shift;
  161. return if (!$device);
  162. my $device_id = $device->{device_id};
  163. my $custom_cfg;
  164. my $device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device->{name}.".cfg";
  165. if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
  166. $device_custom_cfg = $config_ref{nagios_dir}."/custom-cfg/".$device_id.".cfg";
  167. if (-e $device_custom_cfg) { $custom_cfg = read_host_template($device,$device_custom_cfg); }
  168. my $default_service="local-service";
  169. if (!$device->{ou}->{nagios_dir}) { print Dumper($device); }
  170. my $snmp_string = $device->{snmp}->{'ro-community'};
  171. if ($device->{snmp}->{version} > 2) {
  172. $snmp_string = join("::",$device->{snmp}->{'ro-password'},$device->{snmp}->{version},$device->{snmp}->{'ro-user'},$device->{snmp}->{'auth-proto'},$device->{snmp}->{'priv-proto'});
  173. }
  174. my $cfg_file = $device->{ou}->{nagios_dir}."/".$device->{name}.".cfg";
  175. if ($custom_cfg->{template}) {
  176. open(FH, "> $cfg_file");
  177. my @custom_cfg = @{$custom_cfg->{template}};
  178. if (@custom_cfg and scalar(@custom_cfg)) {
  179. foreach my $row (@custom_cfg) {
  180. next if (!$row);
  181. print(FH $row."\n");
  182. }
  183. }
  184. close(FH);
  185. return;
  186. }
  187. #router | switch | gateway
  188. if (in_array([0,1,2],$device->{type})) {
  189. open(FH, "> $cfg_file");
  190. my $device_template = 'switches';
  191. if (in_array([0,2],$device->{type})) { $device_template='routers'; }
  192. print(FH "define host {\n");
  193. print(FH " use $device_template\n");
  194. print(FH " host_name $device->{name}\n");
  195. print(FH " alias $device->{name}\n");
  196. print(FH " address $device->{ip}\n");
  197. print(FH " _ID $device->{device_id}\n");
  198. print(FH " _TYPE device\n");
  199. if ($device->{device_model}) {
  200. print(FH " notes $device->{device_model}->{model_name}\n");
  201. }
  202. if ($device->{parent_name}) {
  203. print(FH " parents $device->{parent_name}\n");
  204. }
  205. print(FH " notes_url ".$config_ref{stat_url}."/admin/devices/editdevice.php?id=$device->{device_id}\n");
  206. print(FH " }\n\n");
  207. #ping
  208. print(FH "define service{\n");
  209. print(FH " use ping-service ; Name of service template to use\n");
  210. print(FH " host_name $device->{name}\n");
  211. print(FH " service_description ping $device->{name}\n");
  212. print(FH " check_command check_ping!100.0,20%!500.0,60%\n");
  213. print(FH " }\n");
  214. #uptime
  215. if ($device->{snmp}->{version}) {
  216. print(FH "define service{\n");
  217. print(FH " use $default_service\n");
  218. print(FH " host_name $device->{name}\n");
  219. print(FH " service_description Uptime\n");
  220. print(FH " check_command check_snmp_uptime!$snmp_string\n");
  221. print(FH " }\n");
  222. print(FH "\n");
  223. #uplink
  224. if (exists $device->{uplink}) {
  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 = $device->{parent_name};
  229. my $conn = $device->{uplink};
  230. print(FH " service_description port $conn->{port} - $port_description CRC Errors\n");
  231. print(FH " check_command check_snmp_switch_crc!$snmp_string!$conn->{snmp_index}\n");
  232. print(FH " }\n\n");
  233. }
  234. foreach my $conn (@{$device->{downlinks}}) {
  235. #id,port,snmp_index,comment
  236. print(FH "define service{\n");
  237. print(FH " use $default_service\n");
  238. print(FH " host_name $device->{name}\n");
  239. my $port_description=translit($conn->{comment});
  240. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  241. print(FH " service_description port $conn->{port} - $port_description \n");
  242. if ($device->{snmp}->{version}<3) {
  243. print(FH " check_command check_ifoperstatus!$conn->{snmp_index}!$device->{snmp}->{'ro-community'}\n");
  244. } else {
  245. 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");
  246. }
  247. print(FH " }\n\n");
  248. #src
  249. print(FH "define service{\n");
  250. print(FH " use service-snmp-crc\n");
  251. print(FH " host_name $device->{name}\n");
  252. my $port_description=translit($conn->{comment});
  253. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  254. print(FH " service_description port $conn->{port} - $port_description CRC Errors\n");
  255. print(FH " check_command check_snmp_switch_crc!$snmp_string!$conn->{snmp_index}\n");
  256. print(FH " }\n\n");
  257. #band
  258. print(FH "define service{\n");
  259. print(FH " use service-snmp-bandwidth\n");
  260. print(FH " host_name $device->{name}\n");
  261. my $port_description=translit($conn->{comment});
  262. if ($conn->{target_port_id}) { $port_description = $conn->{downlink_name}; }
  263. print(FH " service_description port $conn->{port} - $port_description bandwidth usage\n");
  264. print(FH " check_command check_snmp_bandwidth!$snmp_string!$conn->{snmp_index}\n");
  265. print(FH " }\n\n");
  266. }
  267. }
  268. close FH;
  269. }
  270. #auth record
  271. if ($device->{type} eq 3) {
  272. my $cfg_file = print_single_host($device);
  273. open(FH, ">> $cfg_file");
  274. my $dev_cfg;
  275. 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}); }
  276. if ($dev_cfg and $dev_cfg->{template}) {
  277. my @dev_cfg = @{$dev_cfg->{template}};
  278. if (@dev_cfg and scalar(@dev_cfg)) {
  279. foreach my $row (@dev_cfg) {
  280. next if (!$row);
  281. print(FH $row."\n");
  282. }
  283. }
  284. }
  285. close FH;
  286. }
  287. }
  288. #---------------------------------------------------------------------------------
  289. 1;
  290. }