aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring
diff options
context:
space:
mode:
Diffstat (limited to 'modules/private/monitoring')
-rw-r--r--modules/private/monitoring/conf/contacts.cfg41
-rw-r--r--modules/private/monitoring/conf/hosts.cfg32
-rw-r--r--modules/private/monitoring/conf/local_services.cfg68
-rw-r--r--modules/private/monitoring/conf/notify.cfg8
-rw-r--r--modules/private/monitoring/conf/objects.cfg84
-rw-r--r--modules/private/monitoring/conf/services.cfg27
-rw-r--r--modules/private/monitoring/conf/timeperiods.cfg15
-rw-r--r--modules/private/monitoring/default.nix111
-rwxr-xr-xmodules/private/monitoring/plugins/check_command113
-rwxr-xr-xmodules/private/monitoring/plugins/check_mem.sh29
-rwxr-xr-xmodules/private/monitoring/plugins/notify_by_email31
-rwxr-xr-xmodules/private/monitoring/plugins/send_nrdp.sh267
12 files changed, 826 insertions, 0 deletions
diff --git a/modules/private/monitoring/conf/contacts.cfg b/modules/private/monitoring/conf/contacts.cfg
new file mode 100644
index 0000000..e088f69
--- /dev/null
+++ b/modules/private/monitoring/conf/contacts.cfg
@@ -0,0 +1,41 @@
1# vim: filetype=nagios
2
3# CONTACT GROUPS
4define contactgroup {
5 contactgroup_name admins
6 alias Naemon Administrators
7# members immae
8}
9
10# No contact, we go through master
11# define contact {
12# contact_name immae
13# alias Immae
14# use generic-contact
15# email ismael@bouya.org
16# }
17#
18# define contact {
19# name generic-contact
20# host_notification_commands notify-host-by-email
21# host_notification_options d,u,r,f,s
22# host_notification_period 24x7
23# register 0
24# service_notification_commands notify-service-by-email
25# service_notification_options w,u,c,r,f,s
26# service_notification_period 24x7
27# }
28#
29# define command {
30# command_name notify-host-by-email
31# command_line SERVICENOTIFICATIONID="$SERVICENOTIFICATIONID$" HOSTSTATE="$HOSTSTATE$" HOSTOUTPUT="$HOSTOUTPUT$" $USER2$/notify_by_email host "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$LONGDATETIME$" "$CONTACTEMAIL$" $OVE
32# #$OVE is to force naemon to run via shell instead of execve which fails here
33# }
34#
35# # 'notify-service-by-email' command definition
36# define command {
37# command_name notify-service-by-email
38# command_line SERVICENOTIFICATIONID="$SERVICENOTIFICATIONID$" SERVICEDESC="$SERVICEDESC$" SERVICESTATE="$SERVICESTATE$" SERVICEOUTPUT="$SERVICEOUTPUT$" $USER2$/notify_by_email service "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$LONGDATETIME$" "$CONTACTEMAIL$" $OVE
39# # command_line sudo /usr/bin/strace -o /tmp/foo -vf -s 256 -u naemon $USER2$/notify_by_email
40# #$OVE is to force naemon to run via shell instead of execve which fails here
41# }
diff --git a/modules/private/monitoring/conf/hosts.cfg b/modules/private/monitoring/conf/hosts.cfg
new file mode 100644
index 0000000..d903b0a
--- /dev/null
+++ b/modules/private/monitoring/conf/hosts.cfg
@@ -0,0 +1,32 @@
1# vim: filetype=nagios
2
3define host {
4 name generic-host
5 event_handler_enabled 1
6 flap_detection_enabled 1
7 notification_period 24x7
8 notifications_enabled 1
9 process_perf_data 1
10 register 0
11 retain_nonstatus_information 1
12 retain_status_information 1
13}
14
15define host {
16 name linux-server
17 use generic-host
18 check_command check-host-alive
19 check_interval 5
20 check_period 24x7
21 contact_groups admins
22 max_check_attempts 10
23 notification_interval 120
24 notification_options d,u,r,f
25 register 0
26 retry_interval 1
27}
28
29define command {
30 command_name check-host-alive
31 command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
32}
diff --git a/modules/private/monitoring/conf/local_services.cfg b/modules/private/monitoring/conf/local_services.cfg
new file mode 100644
index 0000000..56bc8f6
--- /dev/null
+++ b/modules/private/monitoring/conf/local_services.cfg
@@ -0,0 +1,68 @@
1# vim: filetype=nagios
2
3# System usage
4define service {
5 service_description Size on root partition
6 use local-service
7 check_command check_local_disk!20%!10%!/
8}
9define command {
10 command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
11 command_name check_local_disk
12}
13
14define service {
15 service_description Total number of process
16 use local-service
17 check_command check_local_procs!250!400!RSZDT
18}
19define command {
20 command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
21 command_name check_local_procs
22}
23
24define service {
25 service_description Average load
26 use local-service
27 check_command check_local_load!8.0,8.0,8.0!10.0,10.0,10.0
28}
29define command {
30 command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
31 command_name check_local_load
32}
33
34define service {
35 service_description Swap usage
36 use local-service
37 check_command check_local_swap!20!10
38}
39define command {
40 command_line $USER1$/check_swap -n ok -w $ARG1$ -c $ARG2$
41 command_name check_local_swap
42}
43
44define service {
45 service_description Memory usage
46 use local-service
47 check_command check_memory!80!90
48}
49define command {
50 command_line $USER2$/check_mem.sh -w $ARG1$ -c $ARG2$
51 command_name check_memory
52}
53
54define command {
55 command_line $USER2$/check_command -c "$ARG1$" -s 0 -o "$ARG2$" $ARG3$
56 command_name check_command_output
57}
58
59# Network dependent local services
60define service {
61 service_description NTP is activated and working
62 use local-service
63 check_command check_ntp
64}
65define command {
66 command_line $USER1$/check_ntp_time -t 30 -q -H 0.arch.pool.ntp.org
67 command_name check_ntp
68}
diff --git a/modules/private/monitoring/conf/notify.cfg b/modules/private/monitoring/conf/notify.cfg
new file mode 100644
index 0000000..63b380d
--- /dev/null
+++ b/modules/private/monitoring/conf/notify.cfg
@@ -0,0 +1,8 @@
1# vim: filetype=nagios
2
3define command {
4 command_line /etc/naemon/send_nrdp.sh -H "$HOSTADDRESS$" -s "$SERVICEDESC$" -S "$SERVICESTATEID$" -o "$SERVICEOUTPUT$"
5 command_name notify-master
6}
7
8
diff --git a/modules/private/monitoring/conf/objects.cfg b/modules/private/monitoring/conf/objects.cfg
new file mode 100644
index 0000000..653477f
--- /dev/null
+++ b/modules/private/monitoring/conf/objects.cfg
@@ -0,0 +1,84 @@
1# vim: filetype=nagios
2
3define command {
4 command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
5 command_name check-host-alive
6}
7
8define command {
9 command_line $USER2$/check_md_raid
10 command_name check_md_raid
11}
12
13define command {
14 command_line $USER2$/check_command -c "$ARG1$" -o "$ARG2$" $ARG3$
15 command_name check_command_output
16}
17
18
19define command {
20 command_line /usr/bin/sudo -u postgres $USER2$/check_postgres_replication "$ARG1$" "$ARG2$" "$ARG3$"
21 command_name check_postgresql_replication
22}
23
24define service {
25 ## --PUPPET_NAME-- (called '_naginator_name' in the manifest) Databases are present in postgresql
26 active_checks_enabled 1
27 check_command check_command_output!psql -c 'select nspname from pg_catalog.pg_namespace'!public!-r postgres
28 check_freshness 0
29 check_interval 5
30 check_period 24x7
31 contact_groups admins
32 event_handler_enabled 1
33 flap_detection_enabled 1
34 host_name caldance-1.v.immae.eu
35 is_volatile 0
36 max_check_attempts 4
37 notification_interval 60
38 notification_options w,u,c,r
39 notification_period 24x7
40 notifications_enabled 0
41 obsess_over_service 1
42 passive_checks_enabled 1
43 process_perf_data 1
44 retain_nonstatus_information 1
45 retain_status_information 1
46 retry_interval 1
47 service_description Databases are present in postgresql
48}
49
50define command {
51 command_line $USER2$/check_last_file_date "$ARG1$" "$ARG2$" "$ARG3$"
52 command_name check_last_file_date
53}
54
55define command {
56 command_line $USER2$/check_date "$ARG1$" "$ARG2$" "$ARG3$"
57 command_name check_date
58}
59
60define service {
61 ## --PUPPET_NAME-- (called '_naginator_name' in the manifest) Postgresql replication for backup-1 is up to date
62 active_checks_enabled 1
63 check_command check_postgresql_replication!backup-1!/run/postgresql!5432
64 check_freshness 0
65 check_interval 5
66 check_period 24x7
67 contact_groups admins
68 event_handler_enabled 1
69 flap_detection_enabled 1
70 host_name caldance-1.v.immae.eu
71 is_volatile 0
72 max_check_attempts 4
73 notification_interval 60
74 notification_options w,u,c,r
75 notification_period 24x7
76 notifications_enabled 0
77 obsess_over_service 1
78 passive_checks_enabled 1
79 process_perf_data 1
80 retain_nonstatus_information 1
81 retain_status_information 1
82 retry_interval 1
83 service_description Postgresql replication for backup-1 is up to date
84}
diff --git a/modules/private/monitoring/conf/services.cfg b/modules/private/monitoring/conf/services.cfg
new file mode 100644
index 0000000..0740dc7
--- /dev/null
+++ b/modules/private/monitoring/conf/services.cfg
@@ -0,0 +1,27 @@
1# vim: filetype=nagios
2
3define service {
4 name generic-service
5 active_checks_enabled 1
6 check_freshness 0
7 check_interval 10
8 check_period 24x7
9 contact_groups admins
10 event_handler_enabled 1
11 flap_detection_enabled 1
12 is_volatile 0
13 max_check_attempts 3
14 notification_interval 60
15 notification_options w,u,c,r,f
16 notification_period 24x7
17 # no notification since we send them to master
18 notifications_enabled 0
19 obsess_over_service 1
20 passive_checks_enabled 1
21 process_perf_data 1
22 register 0
23 retain_nonstatus_information 1
24 retain_status_information 1
25 retry_interval 2
26}
27
diff --git a/modules/private/monitoring/conf/timeperiods.cfg b/modules/private/monitoring/conf/timeperiods.cfg
new file mode 100644
index 0000000..5ffe4ca
--- /dev/null
+++ b/modules/private/monitoring/conf/timeperiods.cfg
@@ -0,0 +1,15 @@
1# vim: filetype=nagios
2
3define timeperiod {
4 alias 24 Hours A Day, 7 Days A Week
5 friday 00:00-24:00
6 monday 00:00-24:00
7 saturday 00:00-24:00
8 sunday 00:00-24:00
9 thursday 00:00-24:00
10 timeperiod_name 24x7
11 tuesday 00:00-24:00
12 wednesday 00:00-24:00
13}
14
15
diff --git a/modules/private/monitoring/default.nix b/modules/private/monitoring/default.nix
new file mode 100644
index 0000000..11861ad
--- /dev/null
+++ b/modules/private/monitoring/default.nix
@@ -0,0 +1,111 @@
1{ config, myconfig, pkgs, lib, ... }:
2let
3 myplugins = pkgs.runCommand "buildplugins" {
4 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
5 } ''
6 mkdir $out
7 cp ${./plugins}/* $out/
8 patchShebangs $out
9 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
10 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
11 pkgs.curl pkgs.which pkgs.coreutils
12 ]}
13 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
14 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
15 ]}
16 '';
17in
18{
19 options = {
20 myServices.monitoring.enable = lib.mkOption {
21 type = lib.types.bool;
22 default = false;
23 description = ''
24 Whether to enable monitoring.
25 '';
26 };
27 };
28
29 config = lib.mkIf config.myServices.monitoring.enable {
30 security.sudo.extraRules = [
31 {
32 commands = [
33 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
34 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
35 ];
36 users = [ "naemon" ];
37 runAs = "root";
38 }
39 ];
40 environment.etc."mdadm.conf" = {
41 enable = true;
42 mode = "0644";
43 user = "root";
44 text = "MAILADDR naemon@immae.eu";
45 };
46
47 # needed since extraResource is not in the closure
48 systemd.services.naemon.path = [ myplugins ];
49 services.naemon = {
50 enable = true;
51 extraConfig = ''
52 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
53 use_syslog=1
54 log_initial_states=1
55 date_format=iso8601
56 admin_email=naemon@immae.eu
57
58 obsess_over_services=1
59 ocsp_command=notify-master
60 '';
61 extraResource = ''
62 $USER2$=${myplugins}
63 $USER200$=${myconfig.env.monitoring.status_url}
64 $USER201$=${myconfig.env.monitoring.status_token}
65 '';
66 objectDefs = builtins.readFile ./conf/local_services.cfg
67 + builtins.readFile ./conf/timeperiods.cfg
68 + builtins.readFile ./conf/services.cfg
69 + builtins.readFile ./conf/contacts.cfg
70 + builtins.readFile ./conf/hosts.cfg
71 + ''
72 define command {
73 command_line ${myplugins}/send_nrdp.sh -u "$USER200$" -t "$USER201$" -H "$HOSTADDRESS$" -s "$SERVICEDESC$" -S "$SERVICESTATEID$" -o "$SERVICEOUTPUT$"
74 command_name notify-master
75 }
76 define service {
77 service_description No mdadm array is degraded
78 use local-service
79 check_command check_command_output!${pkgs.mdadm}/bin/mdadm --monitor --scan -1!^$!-s 0 -r root
80 }
81
82 define service {
83 service_description mailq is empty
84 use local-service
85 check_command check_mailq
86 }
87
88 define command {
89 command_name check_mailq
90 command_line $USER1$/check_mailq -s -w 1 -c 2
91 }
92
93 define service {
94 name local-service
95 use generic-service
96 host_name eldiron.immae.eu
97 check_interval 5
98 max_check_attempts 4
99 register 0
100 retry_interval 1
101 }
102 define host {
103 host_name eldiron.immae.eu
104 alias eldiron.immae.eu
105 address eldiron.immae.eu
106 use linux-server
107 }
108 '';
109 };
110 };
111}
diff --git a/modules/private/monitoring/plugins/check_command b/modules/private/monitoring/plugins/check_command
new file mode 100755
index 0000000..55779fd
--- /dev/null
+++ b/modules/private/monitoring/plugins/check_command
@@ -0,0 +1,113 @@
1#!/usr/bin/env perl
2
3use strict;
4use Getopt::Std;
5$| = 1;
6
7my %opts;
8getopts('hr:C:c:s:o:', \%opts);
9
10my $STATE_OK = 0;
11my $STATE_WARNING = 1;
12my $STATE_CRITICAL = 2;
13my $STATE_UNKNOWN = 3;
14
15if ($opts{'h'} || scalar(%opts) == 0) {
16 &print_help();
17 exit($STATE_OK);
18}
19
20my $command = $opts{'c'};
21if ($command eq '') {
22 print "You must provide a command to check.\n";
23 exit($STATE_UNKNOWN);
24}
25
26my $expected_output = $opts{'o'};
27my $expected_status = $opts{'s'};
28my $other_command = $opts{'C'};
29
30if ($other_command eq '' and $expected_status eq '' and $expected_output eq '') {
31 $expected_status = 0;
32}
33
34my $cmd = $command . ' 2>&1';
35my $other_cmd;
36if ($other_command ne '') {
37 $other_cmd = $other_command . ' 2>&1';
38}
39
40my $run_as;
41if ($opts{'r'}) {
42 $run_as = $opts{'r'};
43 $cmd = "sudo -u $run_as -n $cmd";
44
45 if ($other_command ne '') {
46 $other_cmd = "sudo -u $run_as -n $other_cmd";
47 }
48
49}
50
51my $cmd_result = `$cmd`;
52my $other_cmd_result;
53if ($other_command ne '') {
54 $other_cmd_result = `$other_cmd`;
55 chomp($other_cmd_result);
56}
57
58chomp($cmd_result);
59if ($cmd_result =~ /sudo/i) {
60 print "$command CRITICAL - No sudo right to run the command\n";
61 exit($STATE_UNKNOWN);
62} elsif ($expected_status ne '') {
63 if ($? != $expected_status) {
64 print "$command CRITICAL - Response status $?\n";
65 exit($STATE_CRITICAL);
66 } else {
67 print "$command OK - Response status $?\n";
68 exit($STATE_OK);
69 }
70} elsif ($other_command ne '') {
71 if ($cmd_result ne $other_cmd_result) {
72 print "$command CRITICAL - Expected output not matching other command output\n";
73 exit($STATE_CRITICAL);
74 } else {
75 print "$command OK - Expected output matching other command output\n";
76 exit($STATE_OK);
77 }
78} else {
79 if ($cmd_result !~ /$expected_output/) {
80 print "$command CRITICAL - Expected output not matching\n";
81 exit($STATE_CRITICAL);
82 } else {
83 print "$command OK - Expected output matching\n";
84 exit($STATE_OK);
85 }
86}
87
88sub print_help() {
89 print << "EOF";
90Check whether the given command responds as expected. One of -o -C or -s must be selected.
91
92Options:
93-h
94 Print detailed help screen
95
96-c
97 command to run (required)
98
99-C
100 other command to compare output
101
102-r user
103 Run as user via sudo.
104
105-s
106 status code to check
107
108-o
109 output to check
110
111EOF
112}
113
diff --git a/modules/private/monitoring/plugins/check_mem.sh b/modules/private/monitoring/plugins/check_mem.sh
new file mode 100755
index 0000000..cc97ae2
--- /dev/null
+++ b/modules/private/monitoring/plugins/check_mem.sh
@@ -0,0 +1,29 @@
1#!/bin/bash
2
3if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && [ "$3" = "-c" ] && [ "$4" -gt "0" ]; then
4 FreeM=`free -m`
5 memTotal_m=`echo "$FreeM" |grep Mem |awk '{print $2}'`
6 memUsed_m=`echo "$FreeM" |grep Mem |awk '{print $3}'`
7 memFree_m=`echo "$FreeM" |grep Mem |awk '{print $4}'`
8 memBuffer_m=`echo "$FreeM" |grep Mem |awk '{print $6}'`
9 memCache_m=`echo "$FreeM" |grep Mem |awk '{print $7}'`
10 memUsedPrc=`echo $((($memUsed_m*100)/$memTotal_m))||cut -d. -f1`
11 if [ "$memUsedPrc" -ge "$4" ]; then
12 echo "Memory: CRITICAL Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|TOTAL=$memTotal_m;;;; USED=$memUsed_m;;;; CACHE=$memCache_m;;;; BUFFER=$memBuffer_m;;;;"
13 exit 2
14 elif [ "$memUsedPrc" -ge "$2" ]; then
15 echo "Memory: WARNING Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|TOTAL=$memTotal_m;;;; USED=$memUsed_m;;;; CACHE=$memCache_m;;;; BUFFER=$memBuffer_m;;;;"
16 exit 1
17 else
18 echo "Memory: OK Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used|TOTAL=$memTotal_m;;;; USED=$memUsed_m;;;; CACHE=$memCache_m;;;; BUFFER=$memBuffer_m;;;;"
19 exit 0
20 fi
21else # If inputs are not as expected, print help.
22 sName="`echo $0|awk -F '/' '{print $NF}'`"
23 echo -e "\n\n\t\t### $sName Version 2.0###\n"
24 echo -e "# Usage:\t$sName -w <warnlevel> -c <critlevel>"
25 echo -e "\t\t= warnlevel and critlevel is percentage value without %\n"
26 echo "# EXAMPLE:\t/usr/lib64/nagios/plugins/$sName -w 80 -c 90"
27 echo -e "\nCopyright (C) 2012 Lukasz Gogolin (lukasz.gogolin@gmail.com), improved by Nestor 2015\n\n"
28 exit
29fi
diff --git a/modules/private/monitoring/plugins/notify_by_email b/modules/private/monitoring/plugins/notify_by_email
new file mode 100755
index 0000000..ad0dcc7
--- /dev/null
+++ b/modules/private/monitoring/plugins/notify_by_email
@@ -0,0 +1,31 @@
1#!/usr/bin/env bash
2
3# $1 = service/host
4
5# $2 = type (PROBLEM RECOVERY ACKNOWLEDGEMENT FLAPPINGSTART FLAPPINGSTOP FLAPPINGDISABLED DOWNTIMESTART DOWNTIMESTOP DOWNTIMECANCELLED)
6# http://www.naemon.org/documentation/usersguide/macrolist.html#notificationtype
7
8# $3 = host alias
9
10# $4 = date (YYYY-MM-DDTHH:MM:SS)
11
12# $5 = E-mail
13
14NOTIFICATION_TYPE="$2"
15HOST_ALIAS="$3"
16DATE="$4"
17CONTACT="$5"
18
19message=""
20
21if [ "$1" = "host" ]; then
22 message=$(printf "%b" "***** Naemon *****\n\nNotification Type: $NOTIFICATION_TYPE\n\nHost: $HOST_ALIAS\nState: $HOSTSTATE\nInfo: $HOSTOUTPUT\n\nDate/Time: $DATE\n")
23 subject="** $NOTIFICATION_TYPE Host Alert: $HOST_ALIAS is $HOSTSTATE **"
24else
25 message=$(printf "%b" "***** Naemon *****\n\nNotification Type: $NOTIFICATION_TYPE\n\nService: $SERVICEDESC\nHost: $HOST_ALIAS\nState: $SERVICESTATE\n\nDate/Time: $DATE\n\nAdditional Info:\n\n$SERVICEOUTPUT\n")
26 subject="** $NOTIFICATION_TYPE Service Alert: $HOST_ALIAS/$SERVICEDESC is $SERVICESTATE **"
27fi
28
29# sendwait waits for sendmail to finish its job, otherwise it continues in the
30# background and gets killed too early
31echo "$message" | MAILRC=/dev/null mail -r "naemon@immae.eu" -n -Ssendwait -s "$subject" "$CONTACT"
diff --git a/modules/private/monitoring/plugins/send_nrdp.sh b/modules/private/monitoring/plugins/send_nrdp.sh
new file mode 100755
index 0000000..27e47b4
--- /dev/null
+++ b/modules/private/monitoring/plugins/send_nrdp.sh
@@ -0,0 +1,267 @@
1#!/bin/bash
2#
3# check_nrdp.sh
4#
5# Copyright (c) 2010-2017 - Nagios Enterprises, LLC.
6# Written by: Scott Wilkerson (nagios@nagios.org)
7#
8# 2017-09-25 Troy Lea aka BOX293
9# - Fixed script not working with arguments when run as a cron job
10# or if being used as a nagios command like obsessive compulsive.
11# ... "if [ ! -t 0 ]" was the reason why.
12# 2017-12-08 Jørgen van der Meulen (Conclusion Xforce)
13# - Fixed typo in NRDP abbreviation
14
15
16PROGNAME=$(basename $0)
17RELEASE="Revision 0.6.1"
18
19print_release() {
20 echo "$RELEASE"
21}
22
23print_usage() {
24 echo ""
25 echo "$PROGNAME $RELEASE - Send NRDP script for Nagios"
26 echo ""
27 echo "Usage: send_nrdp.sh -u URL -t token [options]"
28 echo ""
29 echo "Usage: $PROGNAME -h display help"
30 echo ""
31}
32
33print_help() {
34 print_usage
35 echo ""
36 echo "This script is used to send NRDP data to a Nagios server"
37 echo ""
38 echo "Required:"
39 echo " -u"," URL of NRDP server. Usually http://<IP_ADDRESS>/nrdp/"
40 echo " -t"," Shared token. Must be the same token set in NRDP Server"
41 echo ""
42 echo "Options:"
43 echo " Single Check:"
44 echo " -H host name"
45 echo " -s service name"
46 echo " -S State"
47 echo " -o output"
48 echo ""
49 echo " STDIN:"
50 echo " [-d delimiter] (default -d \"\\t\")"
51 echo " With only the required parameters $PROGNAME is capable of"
52 echo " processing data piped to it either from a file or other"
53 echo " process. By default, we use \t as the delimiter however this"
54 echo " may be specified with the -d option data should be in the"
55 echo " following formats one entry per line."
56 echo " For Host checks:"
57 echo " hostname State output"
58 echo " For Service checks"
59 echo " hostname servicename State output"
60 echo ""
61 echo " File:"
62 echo " -f /full/path/to/file"
63 echo " This file will be sent to the NRDP server specified in -u"
64 echo " The file should be an XML file in the following format"
65 echo " ##################################################"
66 echo ""
67 echo " <?xml version='1.0'?>"
68 echo " <checkresults>"
69 echo " <checkresult type=\"host\" checktype=\"1\">"
70 echo " <hostname>YOUR_HOSTNAME</hostname>"
71 echo " <state>0</state>"
72 echo " <output>OK|perfdata=1.00;5;10;0</output>"
73 echo " </checkresult>"
74 echo " <checkresult type=\"service\" checktype=\"1\">"
75 echo " <hostname>YOUR_HOSTNAME</hostname>"
76 echo " <servicename>YOUR_SERVICENAME</servicename>"
77 echo " <state>0</state>"
78 echo " <output>OK|perfdata=1.00;5;10;0</output>"
79 echo " </checkresult>"
80 echo " </checkresults>"
81 echo " ##################################################"
82 echo ""
83 echo " Directory:"
84 echo " -D /path/to/temp/dir"
85 echo " This is a directory that contains XML files in the format"
86 echo " above. Additionally, if the -d flag is specified, $PROGNAME"
87 echo " will create temp files here if the server could not be reached."
88 echo " On additional calls with the same -D path, if a connection to"
89 echo " the server is successful, all temp files will be sent."
90 exit 0
91}
92
93send_data() {
94 pdata="token=$token&cmd=submitcheck"
95 if [ $file ]; then
96 fdata="--data-urlencode XMLDATA@$file"
97 rslt=`curl -f --silent --insecure -d "$pdata" $fdata "$url/"`
98 else
99 pdata="$pdata&XMLDATA=$1"
100 rslt=`curl -f --silent --insecure -d "$pdata" "$url/"`
101 fi
102
103 ret=$?
104
105 status=`echo $rslt | sed -n 's|.*<status>\(.*\)</status>.*|\1|p'`
106 message=`echo $rslt | sed -n 's|.*<message>\(.*\)</message>.*|\1|p'`
107 if [ $ret != 0 ];then
108 echo "ERROR: could not connect to NRDP server at $url"
109 # verify we are not processing the directory already and then write to the directory
110 if [ ! "$2" ] && [ $directory ];then
111 if [ ! -d "$directory" ];then
112 mkdir -p "$directory"
113 fi
114 # This is where we write to the tmp directory
115 echo $xml > `mktemp $directory/nrdp.XXXXXX`
116 fi
117 exit 1
118 fi
119
120 if [ "$status" != "0" ];then
121 # This means we couldn't connect to NRPD server
122 echo "ERROR: The NRDP Server said $message"
123 # verify we are not processing the directory already and then write to the directory
124 if [ ! "$2" ] && [ $directory ];then
125 if [ ! -d "$directory" ];then
126 mkdir -p "$directory"
127 fi
128 # This is where we write to the tmp directory
129 echo $xml > `mktemp $directory/nrdp.XXXXXX`
130 fi
131
132 exit 2
133 fi
134
135 # If this was a directory call and was successful, remove the file
136 if [ $2 ] && [ "$status" == "0" ];then
137 rm -f "$2"
138 fi
139
140 # If we weren't successful error
141 if [ $ret != 0 ];then
142 echo "exited with error "$ret
143 exit $ret
144 fi
145}
146
147while getopts "u:t:H:s:S:o:f:d:c:D:hv" option
148do
149 case $option in
150 u) url=$OPTARG ;;
151 t) token=$OPTARG ;;
152 H) host=$OPTARG ;;
153 s) service=$OPTARG ;;
154 S) State=$OPTARG ;;
155 o) output=$OPTARG ;;
156 f) file=$OPTARG ;;
157 d) delim=$OPTARG ;;
158 c) checktype=$OPTARG ;;
159 D) directory=$OPTARG ;;
160 h) print_help 0;;
161 v) print_release
162 exit 0 ;;
163 esac
164done
165
166if [ ! $checktype ]; then
167 checktype=1
168fi
169if [ ! $delim ]; then
170 delim=`echo -e "\t"`
171fi
172
173if [ "x$url" == "x" -o "x$token" == "x" ]
174then
175 echo "Usage: send_nrdp -u url -t token"
176 exit 1
177fi
178# detecting curl
179if [[ `which curl` =~ "/curl" ]]
180 then curl=1;
181fi
182
183if [[ ! $curl ]];
184then
185 echo "Either curl or wget are required to run $PROGNAME"
186 exit 1
187fi
188
189checkcount=0
190
191if [ $host ]; then
192 xml=""
193 # we are not getting piped results
194 if [ "$host" == "" ] || [ "$State" == "" ]; then
195 echo "You must provide a host -H and State -S"
196 exit 2
197 fi
198 if [ "$service" != "" ]; then
199 xml="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>"
200 else
201 xml="$xml<checkresult type='host' checktype='$checktype'>"
202 fi
203
204 # urlencode XML special chars
205 output=${output//&/%26}
206 output=${output//</%3C}
207 output=${output//>/%3E}
208
209 xml="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>"
210 checkcount=1
211fi
212
213 # If only url and token have been provided then it is assumed that data is being piped
214########################
215if [[ ! $host && ! $State && ! $file && ! $directory ]]; then
216 xml=""
217 # we know we are being piped results
218 IFS=$delim
219
220 while read -r line ; do
221 arr=($line)
222 if [ ${#arr[@]} != 0 ];then
223 if [[ ${#arr[@]} < 3 ]] || [[ ${#arr[@]} > 4 ]];then
224 echo "ERROR: STDIN must be either 3 or 4 fields long, I found "${#arr[@]}
225 else
226 if [ ${#arr[@]} == 4 ]; then
227 xml="$xml<checkresult type='service' checktype='$checktype'>
228 <servicename>${arr[1]}</servicename>
229 <hostname>${arr[0]}</hostname>
230 <state>${arr[2]}</state>
231 <output>${arr[3]}</output>"
232 else
233 xml="$xml<checkresult type='host' checktype='$checktype'>
234 <hostname>${arr[0]}</hostname>
235 <state>${arr[1]}</state>
236 <output>${arr[2]}</output>"
237 fi
238
239 xml="$xml</checkresult>"
240 checkcount=$[checkcount+1]
241 fi
242 fi
243 done
244 IFS=" "
245fi
246
247if [ $file ]; then
248 xml=`cat $file`
249 send_data "$xml"
250fi
251
252if [ $directory ]; then
253 #echo "Processing directory..."
254 for f in `ls $directory`
255 do
256 #echo "Processing $f file..."
257 # take action on each file. $f store current file name
258 xml=`cat $directory/$f`
259 send_data "$xml" "$directory/$f"
260 done
261fi
262
263if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then
264 xml="<?xml version='1.0'?><checkresults>$xml</checkresults>"
265 send_data "$xml"
266 echo "Sent $checkcount checks to $url"
267fi