aboutsummaryrefslogtreecommitdiffstats
path: root/snmptrap/snmptrapd.sh
blob: a10e0491200bed6c91242060ca3e1853cb6ae88c (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env bash
#
# ============LICENSE_START=======================================================
# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================

# basics
current_cmd=`basename $0`
current_module=`echo $current_cmd | cut -d"." -f1`

# get base_dir from current script invocation
bin_base_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
base_dir=`dirname ${bin_base_dir}`

snmptrapd_pid_file=${base_dir}/tmp/${current_module}.py.pid
scheduler_pid_file=${base_dir}/tmp/scheduler.sh.pid

start_dir=${base_dir}/bin

# global return code
#  - required because functions ultimately call log_msg, which
#    is to stdout, which conflicts with "echo <return_value>"
#    in the functions themselves
g_return=0

# include path to 3.6+ version of python that has required dependencies included
export PATH=/opt/app/python-3.6.1/bin:$PATH

# set location of SSL certificates
# export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-bundle.crt	# open source/external world
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt	# otherwise...

# find the best tool for the job
if [ `command -v pypy3` ]
then
   PY_BINARY=pypy3
else
   if [ `command -v python3` ]
   then
      PY_BINARY=python3
   else
      if [ `command -v python` ]
      then
         PY_BINARY=python
      else
         echo "ERROR: no pypy3 or python available in container - FATAL ERROR, exiting"
         exit 1
      fi
   fi
fi

# expand search for python modules to include ./mod in current/runtime dir
export PYTHONPATH=${bin_base_dir}/mod:$PYTHONPATH

# PYTHONUNBUFFERED:
#    set PYTHONUNBUFFERED to a non-empty string to avoid output buffering; 
#    comment out for runtime environments/better performance!
# export PYTHONUNBUFFERED="True"

# set location of config broker server overrride IF NEEDED
#
export CBS_SIM_JSON=${base_dir}/etc/snmptrapd.json

# misc
exit_after=1

# # # # # # # # # # 
# log_msg - log messages to stdout in standard manner
# # # # # # # # # # 
log_msg()
{
   msg=$*

   echo "`date +%Y-%m-%dT%H:%M:%S,%N | cut -c1-23` ${msg}" 
}

#
# start process
#
start_process()
{
process_name=$1
pid_file=$2
exec_cmd=$3

   # check if exec_cmd has a pid_file 
   if [ ! -r ${pid_file} ]
   then
      log_msg "Starting ${process_name}"
      stdout_fd=${base_dir}/logs/${process_name}.out
      if [ -f ${stdout_fd} ]
      then
         mv -f ${stdout_fd} ${stdout_fd}.bak
      fi
      ${exec_cmd} >> ${base_dir}/logs/${process_name}.out 2>&1 &
      g_return=$?
      echo $! > ${pid_file}
   else
      pid=$(cat ${pid_file})
      if ps -p ${pid} > /dev/null
      then
         g_return=$?
         log_msg "${process_name} already running - PID ${pid}"
      else
         log_msg "PID file present, but no corresponding process.  Starting ${process_name}"
         ${exec_cmd} >> ${base_dir}/logs/${process_name}.out 2>&1 &
         g_return=$?
         echo $! > ${pid_file}
      fi
   fi
}

# # # # # # # # # # 
# Start the service
# # # # # # # # # # 
start_service()
{
   # Hints for startup modifications:
   # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

   # handy for debug (e.g. docker logs output)
   # log_msg "Runtime env present for ${current_module} placed in ${base_dir}/logs/${current_module}.out"
   env >>  ${base_dir}/logs/${current_module}.out

   # standard startup?  Use this:
   cmd="${PY_BINARY} ${base_dir}/bin/snmptrapd.py"
   # want tracing?  Use this:
   # cmd="${PY_BINARY} ./snmptrapd.py -v"
   # unbuffered io for logs? Use this:
   #     cmd="${PY_BINARY} -u ./snmptrapd.py"
   # fmdl: needs further research
   #     cmd="${PY_BINARY} -m trace --trackcalls ./snmptrapd.py"

   cd ${start_dir}

   #
   # scheduler
   #
   start_process scheduler ${scheduler_pid_file} "${bin_base_dir}/scheduler.sh"
   if [ ${g_return} -ne 0 ]
   then
       log_msg "ERROR!  Unable to start scheduler.  Check logs for details."
   fi

   #
   # snmptrapd
   #
   start_process ${current_module} ${snmptrapd_pid_file} "${cmd}"
   if [ ${g_return} -ne 0 ]
   then
       log_msg "ERROR!  Unable to start ${current_module}.  Check logs for details."
   fi
}

# # # # # # # # # # 
# Stop the service
# # # # # # # # # # 
stop_process()
{
process_name=$1
pid_file=$2

    if [ ! -r ${pid_file} ]
    then
        log_msg "PID file ${pid_file} does not exist or not readable - unable to stop ${process_name}"
        g_return=1
    else
        pid=$(cat ${pid_file})
        pgrep -f ${process_name} | grep "^${pid}" > /dev/null
        loc_return=$?
        if [ ${loc_return} -eq 0 ]
        then
            log_msg "Stopping ${process_name} PID ${pid}..."
            kill ${pid}
            g_return=$?
            if [ ${g_return} -eq 0 ]
            then
                log_msg "Stopped"
            else
                log_msg "ERROR while terminating ${process_name} PID ${pid} (is it not running or owned by another userID?)"
            fi
        else
            log_msg "${process_name} PID ${pid} not present - skipping"
        fi

        if [ -w ${pid_file} ]
        then
           rm -f ${pid_file}
        fi
    fi
}

# # # # # # # # # # # # # # #
# stop all snmptrapd services
# # # # # # # # # # # # # # #
stop_service()
{
   # scheduler
   #
   stop_process scheduler ${scheduler_pid_file}

   # snmptrapd
   #
   stop_process ${current_module} ${snmptrapd_pid_file}
}

# # # # # # # # # # # # # # #
# Check status of the service
# # # # # # # # # # # # # # #
status_process()
{
process_name=$1
pid_file=$2

    if [ -r ${pid_file} ]
    then
        pid=$(cat ${pid_file})
        pgrep -f ${process_name} | grep "^${pid}" > /dev/null
        loc_return=$?

        if [ ${loc_return} -eq 0 ]
        then
            log_msg "- ${process_name} running, PID ${pid}"
            # ps -f -p ${pid} -f | grep -v PID
            g_return=0
        else
            log_msg "ERROR! ${process_name} not running"
            g_return=1
        fi
   else
        log_msg "PID file ${pid_file} does not exist or not readable - unable to check status of ${process_name}"
        g_return=1
    fi
}

#
#
#
status_service()
{
   # scheduler
   #
   status_process scheduler  ${scheduler_pid_file}
   loc_return=${g_return}

   # snmptrapd
   #
   status_process ${current_module} ${snmptrapd_pid_file}
   loc_return=$((loc_return+g_return))
   if [ ${loc_return} -ne 0 ]
   then
      log_msg "Overall Status: CRITICAL - Required process(es) missing!"
   else
      log_msg "Overall Status: Normal"
   fi
}

# # # # # # # # # # # # # # # # #
# Signal process to reload config
# # # # # # # # # # # # # # # # #
reload_cfg()
{
    # only needed for snmptrapd
    if [ -r ${snmptrapd_pid_file} ]
    then
       pid=$(cat ${snmptrapd_pid_file})
       ps -p ${pid} > /dev/null 2>&1
       loc_return=$?
       if [ ${loc_return} ]
       then
          log_msg "Signaling ${current_module} PID ${pid} to request/read updated configs..."
          kill -USR1 ${pid}
          g_return=$?
          if [ ${g_return} -eq 0 ]
          then
              log_msg "...Signal complete."
          else
              log_msg "ERROR signaling ${current_module} (do you have permissions to do this?)"
          fi
       else
          log_msg "ERROR: ${current_module} PID ${pid} does not appear to be running."
       fi
    else
       log_msg "ERROR: ${snmptrapd_pid_file} does not exist - unable to signal for config re-read."
       g_return=1
    fi
}

# # # # # # # # # # # # # # #
# stop all snmptrapd services
# # # # # # # # # # # # # # #
version()
{
exit_swt=$1

version_fd=${base_dir}/etc/version.dat
if [ -f ${version_fd} ]
then
   version_string=`cat ${version_fd}`
   log_msg "${version_string}"
   ec=0
else
   log_msg "ERROR: unable to determine version"
   ec=1
fi

if [ "${exit_swt}" == "${exit_after}" ]
then
   exit ${ec}
fi

}

# # # # # # # # # # # # #
# M A I N
# # # # # # # # # # # # #


case "$1" in
   "start")
          version
          start_service
          sleep 1
          status_service
          wait
          ;;
   "stop") 
          version
          stop_service
          ;;
   "restart")
          version
          stop_service
          sleep 1
          start_service
          status_service
          ;;
   "status") 
          version
          status_service
          ;;
   "reloadCfg")
          version
          reload_cfg
          ;;
   "version")
          version ${exit_after}
          ;;
   *)
          echo "Usage: ${current_cmd} {start|stop|restart|status|reloadCfg|version}"
          g_return=1
   esac

exit ${g_return}