-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcreate_nagios_monitor.sh
189 lines (132 loc) · 5.11 KB
/
create_nagios_monitor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
USAGE="
SYNTAX: $0 [-h] -H <hostname> [-i <string ipv4 address>] [-f <string template file path>]
DESCRIPTION:
This script is used to duplicate a Nagios monitoring template in Nagios Core.
After running this script you can start monitoring your newly created device.
REQUIREMENTS:
1.) Template file should NOT end with a .cfg extension to prevent extraneous monitor. New file is saved to same location as the template file
2.) Use the cfg_dir value in /usr/local/nagios/etc/nagios.cfg to define a directory(s) where cfg files become monitoring files
3.) Uses the following default values which this script replaces in the template files. These are the default values of these config files
a.) LINUX: localhost.cfg hostname localhost gets replaced and IP 192.168.1.2 gets replaced
b.) WINDOWS: windows.cfg hostname winserver gets replaced and IP 192.168.1.2 gets replaced and Alias My Windows Server gets replaced
c.) PRINTERS: printer.cfg hostname hplj2605dn gets replaced and IP 192.168.1.2 gets replaced and Alias HP LaserJet 2605dn gets replaced
CONTACT INFORMATION
Company: OsbornePro LLC.
Website: https://osbornepro.com
Author: Robert H. Osborne
Contact: [email protected]
USAGE: $0 -H <hostname> [-i <ipv4 address>] [-f <template file path>]
OPTIONS:
-h : Displays the help information for the command.
-H : Define the hostname to monitor
-i : Set the IPv4 address of the server to monitor
-t : Define the location of the template file
-r : Restarts the Nagios service
EXAMPLES:
$0 -H dc01.domain.com -r
# This example create a Nagios Core monitoring file for dc01.domain.com in /usr/local/nagios/etc/objects/windows_servers/dc01.domain.com.cfg and restarts the Nagios service
$0 -H dhcp.domain.com -i 10.20.11.67 -r
# This example create a Nagios Core monitoring file for dhcp.domain.com in /usr/local/nagios/etc/objects/windows_servers/dhcp.domain.com.cfg and restarts the Nagios service
$0 -H files.domain.com -i 10.20.44.5 -t /usr/local/nagios/etc/objects/windows_servers/windows_template.cfg.orig
# This example create a Nagios Core monitoring file for files.domain.com in /usr/local/nagios/etc/objects/windows_servers/files.domain.com.cfg
"
IPV4=$(dig +short $DNSNAME A | head -n 1)
function allow_ctrlc {
# Allow Ctrl+C to stop execution
trap '
trap - INT # restore default INT handler
kill -s INT "$$"
' INT
} # End function allow_ctrlc
function print_usage {
printf "$USAGE\n" >&2
exit 1
} # End function print_usage
function validate_ipv4 {
printf "[*] Verifying $IPV4 value \n"
IPV4REGEX="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
if [[ "$IPV4" =~ "$IPV4REGEX" ]] || IPERROR="Valid IP subnet was not defined. For more help execute '$0 -h' \n"; then
if [ ! "$IPERROR" ]; then
printf "[x] A valid network value was not defined, you entered $IPV4. Use -h for help info. \n"
exit 1
fi
fi
} # End function validate_ipv4
function get_hostname {
if [ -z $DNSNAME ]; then
printf "[?] Enter the Windows Server's hostname or FQDN you wish to monitor: "
read DNSNAME
fi
if [ -z $DNSNAME ]; then
exit 1
fi
LOWERCASE=$(printf $DNSNAME | tr '[:upper:]' '[:lower:]')
FILENAME=$(printf $TEMPLATEFILE | tr "/" "\n" | tail -n 1)
DESTINATION=$(printf $TEMPLATEFILE | sed "s|$FILENAME|$LOWERCASE.cfg|")
}
function verify_ipaddress {
printf "[?] What is the IP Address? \n Press ENTER to keep the value $IPV4 : "
read IPADDRESS
if [ -z "$IPADDRESS" ]; then
IPV4=$(printf $IPADDRESS)
fi
if [ -z $IPADDRESS ]; then
exit 1
fi
}
function verify_changes {
printf "[*] The below values are going to be set for this server \n"
printf "[i] Hostname: $DNSNAME \n"
printf "[i] IP Address: $IPV4 \n"
printf "[i] Alias: $LOWERCASE \n"
printf "[i] File Path: $DESTINATION \n"
read -p "[*] Press [ENTER] key to continue "
}
function create_cfg {
sed "s|localhost|$LOWERCASE|g; s|winserver|$LOWERCASE|g; s|hplj2605dn|$LOWERCASE|g; s|HP LaserJet 2605dn|$DNSNAME|g; s|My Windows Server|$DNSNAME|g; s|192.168.1.2|$IPV4|g" "$TEMPLATEFILE" > "$DESTINATION"
}
function restart_nagios_service {
TEST=$(/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg)
RESULT=$(echo $TEST | grep "Things look okay - No serious problems were detected during the pre-flight check")
if [ -z "$RESULT" ]; then
printf "[x] There is an issue with your current Nagios configuration \n"
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
else
if [ "$RESTARTSERVICE" == "True" ]; then
systemctl restart nagios.service && systemctl status nagios.service
else
printf "[*] You will need to restart the nagios service to apply your changes. \n[i]\tsystemctl restart nagios.service\n"
fi
fi
}
while [ ! -z "$1" ]; do
case "$1" in
-H)
shift
DNSNAME=$1
;;
-i)
shift
IPV4=$1
;;
-t)
shift
TEMPLATEFILE=$1
;;
-r)
shift
RESTARTSERVICE="True"
;;
*)
print_usage
;;
esac
shift
done
allow_ctrlc
get_hostname
get_ipaddress
verify_changes
create_cfg
restart_nagios_service