nagios.pm 13 KB

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