nagios.pm 13 KB

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