-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-cfg.sh
executable file
·179 lines (146 loc) · 4.3 KB
/
gen-cfg.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
#!/bin/bash
. ./conf
REGION=""
ignore=0
PROJECT=""
ENV=""
ITEM=""
HOST=""
MEMBERS=""
CFG_PATH="${NAGIOS_PATH}/../nagios.cfg"
mode=0
set_cfg() {
CFG_PATH="${NAGIOS_PATH}/${1}"
}
write_config() {
echo "${1}" >> ${CFG_PATH}
}
check_config() {
cat ${CFG_PATH} 2>&1 | grep "${1}" > /dev/null 2>&1
echo $?
}
set_ignore() {
echo ${NEEDREGION} | grep ${REGION} > /dev/null
ignore=${?}
}
configure_host() {
# some project not exist subproject.
if [ "" = "${ITEM}" ]; then
HOST="${ENV}-${PROJECT}"
else
HOST="${PROJECT}-${ENV}-${ITEM}"
fi
if [ 0 -ne $(check_config "${HOST}$") ]; then
write_config "define host {"
write_config " use linux-server"
write_config " host_name ${HOST}"
write_config " address 127.0.0.1"
write_config "}"
write_config ""
fi
MEMBERS="${MEMBERS}${HOST},"
}
configure_hostg() {
if [ 0 -ne $(check_config " ${PROJECT}$") ]; then
write_config "define hostgroup {"
write_config " hostgroup_name ${PROJECT}"
write_config " members ${MEMBERS}"
write_config "}"
write_config ""
else
line=$(( 1 + $(grep -n " ${PROJECT}$" ${CFG_PATH} | cut -d : -f1) ))
MEMBERS="${MEMBERS},$(sed -n ${line}p ${CFG_PATH} | awk '{print $2}')"
MEMBERS="$(echo ${MEMBERS} | sed 's/,/ /g')"
NEW_MEMBERS=""
for host in ${MEMBERS}
do
echo ${NEW_MEMBERS} | grep ${host} > /dev/null
if [ 0 -ne $? ]; then
NEW_MEMBERS="${NEW_MEMBERS}${host},"
fi
done
sed -i "${line}s/.*/ members ${NEW_MEMBERS%,}/" ${CFG_PATH}
fi
}
configure_svc() {
# enhanced
url=${1%%/*}
path=${1#*/}
if [[ ${url} = ${path} ]]; then
path=""
fi
if [ 0 -ne $(check_config "${HOST} ${1}$") ]; then
write_config "define service {"
write_config " use https_check"
write_config " host_name ${HOST}"
write_config " service_description HTTPS ${HOST} ${1}"
write_config " check_command check_https!-H ${url} -u /${path}"
write_config " servicegroups ${PROJECT}-${ENV}"
write_config "}"
write_config ""
# write_config "define service {"
# write_config " use ssl_check"
# write_config " host_name ${HOST}"
# write_config " service_description SSL ${HOST} ${1}"
# write_config " check_command check_ssl!-H ${url}"
# write_config " servicegroups ${PROJECT}-${ENV}"
# write_config "}"
# write_config ""
fi
}
configure_svcg() {
if [ 0 -ne $(check_config "${PROJECT}-${ENV}$") ]; then
write_config "define servicegroup {"
write_config " servicegroup_name ${PROJECT}-${ENV}"
write_config "}"
write_config ""
fi
}
if [ ! -d ${NAGIOS_PATH} ]; then
echo No find directory.
exit 1
fi
if [ 0 -ne $(check_config "${NAGIOS_PATH}") ]; then
write_config "cfg_dir=/etc/nagios/project"
fi
if [ ! -d ${NAGIOS_PATH}/svc ]; then
mkdir ${NAGIOS_PATH}/svc
fi
rm -f ${NAGIOS_PATH}/host.cfg
rm -f ${NAGIOS_PATH}/svcgp.cfg
rm -f ${NAGIOS_PATH}/svc/*.cfg
while read output
do
punct="${output% *}"
if [ "#" = "${punct}" ]; then
REGION=${output#* }
set_ignore
elif [ 0 -eq ${ignore} ]; then
if [ "-" = "${punct}" ]; then
if [ 1 -ne $mode ]; then
set_cfg "host.cfg"
configure_host
set_cfg "svc/${PROJECT}-${ENV}.cfg"
mode=1
fi
configure_svc ${output#* }
elif [ "####" = "${punct}" ]; then
ITEM=${output#* }
elif [ "###" = "${punct}" ]; then
ENV=${output#* }
set_cfg "svcgp.cfg"
configure_svcg
elif [ 0 -ne $mode ]; then
mode=0
elif [ "##" = "${punct}" ]; then
if [ "" != "${MEMBERS}" ]; then
MEMBERS="${MEMBERS%,}"
set_cfg "host.cfg"
configure_hostg
MEMBERS=""
fi
PROJECT=${output#* }
ITEM=""
fi
fi
done < <(cat url.md && echo)