aboutsummaryrefslogtreecommitdiffstats
path: root/snmptrap/scheduler.sh
blob: bb2d08365cf0de8259f2b0b59df7d9bfdbd789e6 (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
#!/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=========================================================
#
#

script_name=`basename "$0"`

# sleep_time=.1		# in seconds
sleep_time=1		# in seconds

log_fd=/var/tmp/${script_name}.log
log_lines=0
max_log_lines=4000

num_days_to_keep_logs=7

# 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

# logging utility
logFx()
{
_fx=$1
# skipping verbosity setting
_verbosity=$2
_log_message=$3

   echo "`date` `date +%s` ${_fx} ${_error_code} ${_log_message}" >> ${log_fd}
   
   # log_lines=`wc -l ${log_fd} | awk {'print $1'} | bc`
   # log_lines=$((${log_lines}+1))
   
   # if [ ${log_lines} -ge ${max_log_lines} ]
   # then
   #    if [ -f ${log_fd} ]
   #    then
   #       mv -f ${log_fd} ${log_fd}.old
   #    fi
   # fi
}

run_10sec_jobs()
{
    # send heartbeat
    ${PY_BINARY} /opt/app/snmptrap/bin/send_hb_trap localhost 6162 > /var/tmp/send_hb_trap.out 2>&1 &
    rc=$?
    logFx ${FUNCNAME[0]} 0 "send_hb_trap returned ${rc}"

    # other 10 second jobs below here

    return ${rc}
}

run_minute_jobs()
{
rc=0
    # add other minute jobs below here

    return ${rc}
}

run_hourly_jobs()
{
    # no hourly jobs scheduled at this time
    rc=0

    # add other hourly jobs below here

    return ${rc}
}

run_daily_jobs()
{
    rc=0

    # remove old logs
    for f in `find /opt/app/snmptrap/logs -type f -mtime +${num_days_to_keep_logs}`
    do
       logFx ${FUNCNAME[0]} 0 "removing $f"
       rm $f
    done

    # move scheduler log_fd to daily backup
    mv -f ${log_fd} ${log_fd}.`date +%a`

    # add other daily jobs below here

    return ${rc}
}


# # # # # # # # # # # # # #
# main HCCCKK area
# # # # # # # # # # # # # #

begin_minute=`date +%M | bc`

# wait for minute to roll to new one
logFx ${FUNCNAME[0]} 0 "waiting for new minute..."
while [ ${begin_minute} -eq `date +%M | bc` ]
do
   sleep .1
done

SECONDS=0
logFx ${FUNCNAME[0]} 0 "scheduler synced to new minute"
last_minute=`date +%M`
last_hour=`date +%H`
last_day=`date +%j`

logFx ${FUNCNAME[0]} 0 "entering endless loop"
while(true)
do
   if [ $SECONDS -ge 10 ]
   then
      # run every 10 seconds jobs
      logFx ${FUNCNAME[0]} 0 "$SECONDS seconds have elapsed - calling run_10sec_jobs"
      run_10sec_jobs
      # reset SECONDS
      SECONDS=0

      # check for minute change
      current_minute=`date +%M | bc`
      if [ ${current_minute} -ne ${last_minute} ]
      then
         # run every minute jobs
         logFx ${FUNCNAME[0]} 0 "minute change from ${last_minute} to ${current_minute} - calling run_minute_jobs"
         run_minute_jobs
         # reset last_minute
         last_minute=${current_minute}

         # check for hour change
         current_hour=`date +%H | bc`
         if [ ${current_hour} -ne ${last_hour} ]
         then
            # run every hour jobs
            logFx ${FUNCNAME[0]} 0 "hour change from ${last_hour} to ${current_hour} - calling run_hourly_jobs"
            run_hourly_jobs
            # reset last_minute
            last_hour=${current_hour}

            # check for day change
            current_day=`date +%j | bc`
            if [ ${current_day} -ne ${last_day} ]
            then
               # run every day jobs
               logFx ${FUNCNAME[0]} 0 "day change from ${last_day} to ${current_day} - calling run_daily_jobs"
               run_daily_jobs
               # reset last_day
               last_day=${current_day}
            fi      
         fi      
      fi      
   fi
   sleep ${sleep_time}
done