File: nagios

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (246 lines) | stat: -rwxr-xr-x 7,286 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/sh
#
#  License:      GNU General Public License (GPL)
#  (c) 2015 T.J. Yang, O. Albrigtsen
#           and Linux-HA contributors
#
# -----------------------------------------------------------------------------
#      O C F    R E S O U R C E    S C R I P T   S P E C I F I C A T I O N
# -----------------------------------------------------------------------------
#
# NAME
#       nagios : OCF resource agent script for Nagios Server
#

# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Defaults
OCF_RESKEY_user_default="nagios"
OCF_RESKEY_group_default="nagios"
OCF_RESKEY_binary_default="/usr/sbin/nagios"
OCF_RESKEY_config_default="/etc/nagios/nagios.cfg"
OCF_RESKEY_log_default="/var/log/nagios/nagios.log"
OCF_RESKEY_retention_default="/var/log/nagios/retention.dat"
OCF_RESKEY_command_default="/var/log/nagios/rw/nagios.cmd"
OCF_RESKEY_pid_default="/var/run/nagios.pid"

: ${OCF_RESKEY_user=${OCF_RESKEY_user_default}}
: ${OCF_RESKEY_group=${OCF_RESKEY_group_default}}
: ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
: ${OCF_RESKEY_config=${OCF_RESKEY_config_default}}
: ${OCF_RESKEY_log=${OCF_RESKEY_log_default}}
: ${OCF_RESKEY_retention=${OCF_RESKEY_retention_default}}
: ${OCF_RESKEY_command=${OCF_RESKEY_command_default}}
: ${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}


nagios_usage() {
  cat <<END
    usage: $0 (start|stop|validate-all|meta-data|help|usage|monitor)
    $0 manages a Nagios instance as an OCF HA resource.
    The 'start' operation starts the instance.
    The 'stop' operation stops the instance.
    The 'status' operation reports whether the instance is running
    The 'monitor' operation reports whether the instance seems to be working
    The 'validate-all' operation reports whether the parameters are valid
END
}

nagios_meta_data() {
        cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="nagios">
<version>0.75</version>

<longdesc lang="en">OCF Resource script for Nagios 3.x or 4.x. It manages a Nagios instance as a HA resource.</longdesc>
<shortdesc lang="en">Nagios resource agent</shortdesc>

<parameters>

<parameter name="user">
    <longdesc lang="en">User running Nagios daemon (for file permissions)</longdesc>
    <shortdesc lang="en">Nagios user</shortdesc>
    <content type="string" default="${OCF_RESKEY_user_default}" />
</parameter>

<parameter name="group">
    <longdesc lang="en">Group running Nagios daemon (for file permissions)</longdesc>
    <shortdesc lang="en">Nagios group</shortdesc>
    <content type="string" default="${OCF_RESKEY_group_default}" />
</parameter>

<parameter name="binary">
    <longdesc lang="en">Location of the Nagios binary</longdesc>
    <shortdesc lang="en">Nagios binary</shortdesc>
    <content type="string" default="${OCF_RESKEY_binary_default}" />
</parameter>

<parameter name="config">
    <longdesc lang="en">Configuration file</longdesc>
    <shortdesc lang="en">Nagios config</shortdesc>
    <content type="string" default="${OCF_RESKEY_config_default}" />
</parameter>

<parameter name="log">
    <longdesc lang="en">Location of the Nagios log</longdesc>
    <shortdesc lang="en">Nagios log</shortdesc>
    <content type="string" default="${OCF_RESKEY_log_default}" />
</parameter>

<parameter name="retention">
    <longdesc lang="en">Location of the Nagios retention file</longdesc>
    <shortdesc lang="en">Nagios retention file</shortdesc>
    <content type="string" default="${OCF_RESKEY_retention_default}" />
</parameter>

<parameter name="command">
    <longdesc lang="en">Location of the Nagios external command file</longdesc>
    <shortdesc lang="en">Nagios command file</shortdesc>
    <content type="string" default="${OCF_RESKEY_command_default}" />
</parameter>

<parameter name="pid">
    <longdesc lang="en">Location of the Nagios pid/lock</longdesc>
    <shortdesc lang="en">Nagios pid file</shortdesc>
    <content type="string" default="${OCF_RESKEY_pid_default}" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="20" />
<action name="stop" timeout="20" />
<action name="status" timeout="20" />
<action name="monitor" depth="0" timeout="20" interval="10" start-delay="10" />
<action name="validate-all" timeout="20" />
<action name="meta-data" timeout="20" />
</actions>
</resource-agent>
END
}


nagios_start() {
    nagios_validate_all
    rc=$?
    if [ $rc -ne 0 ]; then
        return $rc
    fi

    # if resource is already running,no need to continue code after this.
    if nagios_monitor; then
        ocf_log info "Nagios is already running"
        return $OCF_SUCCESS
    fi

    # Remove ${OCF_RESKEY_pid} if it exists
    rm -f "${OCF_RESKEY_pid}"

    ocf_run -q touch ${OCF_RESKEY_log} ${OCF_RESKEY_retention} ${OCF_RESKEY_pid}
    chown ${OCF_RESKEY_user}:${OCF_RESKEY_group} ${OCF_RESKEY_log} ${OCF_RESKEY_retention} ${OCF_RESKEY_pid}
    rm -f "${OCF_RESKEY_command}"

    [ -x /sbin/restorecon ] && /sbin/restorecon ${OCF_RESKEY_pid}
    ocf_run -q ${OCF_RESKEY_binary} -d ${OCF_RESKEY_config}

    while ! nagios_monitor; do
        sleep 1
    done

    if [ $? -eq 0 ]; then
        ocf_log info "Nagios started"
        return ${OCF_SUCCESS}
    fi

    return $OCF_SUCCESS
}

nagios_stop() {
    nagios_monitor
    if [ $? -ne $OCF_SUCCESS ]; then
        # Currently not running. Nothing to do.
        ocf_log info "Resource is already stopped"
        rm -f ${OCF_RESKEY_pid}

        return $OCF_SUCCESS
    fi

    kill `cat ${OCF_RESKEY_pid}`

    # Wait for process to stop
    while nagios_monitor; do
        sleep 1
    done

    return $OCF_SUCCESS
}

nagios_monitor(){
    ocf_pidfile_status ${OCF_RESKEY_pid} > /dev/null 2>&1
    case "$?" in
        0)
            rc=$OCF_SUCCESS
            ;;
        1|2)
            rc=$OCF_NOT_RUNNING
            ;;
        *)
            rc=$OCF_ERR_GENERIC
            ;;
    esac
    return $rc
}

nagios_validate_all(){
    check_binary "${OCF_RESKEY_binary}"

    if [ ! -f "${OCF_RESKEY_config}" ]; then
        ocf_exit_reason "Configuration file ${OCF_RESKEY_config} not found"
        return ${OCF_ERR_INSTALLED}
    fi

    ${OCF_RESKEY_binary} -v ${OCF_RESKEY_config} >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        ocf_exit_reason "Configuration check failed"
        return ${OCF_ERR_INSTALLED}
    fi
}


# **************************** MAIN SCRIPT ************************************

# Make sure meta-data and usage always succeed
case $__OCF_ACTION in
meta-data)      nagios_meta_data
                exit $OCF_SUCCESS
                ;;
usage|help)     nagios_usage
                exit $OCF_SUCCESS
                ;;
esac

# This OCF agent script need to be run as root user.
if ! ocf_is_root; then
        echo  "$0 agent script need to be run as root user."
        ocf_log debug "$0 agent script need to be run as root user."
        exit $OCF_ERR_GENERIC
fi

# Translate each action into the appropriate function call
case $__OCF_ACTION in
start)          nagios_start;;
stop)           nagios_stop;;
status|monitor) nagios_monitor;;
validate-all)   nagios_validate_all;;
*)              nagios_usage
                exit $OCF_ERR_UNIMPLEMENTED
                ;;
esac
rc=$?

exit $rc

# End of this script