-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncwd-service
executable file
·291 lines (245 loc) · 7.21 KB
/
ncwd-service
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
#!/usr/bin/env bash
################################################################################
#title :nordcloud-watchdog
#description :Custom watchdog functionality implemented for Nordcloud
#author :Csaba SARI <scsaba314@gmail.com>
#date :20170925
#version :0.1
#usage :nordcloud-watchdog
#notes :Install Vim and Emacs to use this script.
#source :Taken from https://gist.github.com/shawnrice/11076762
#bash_version :4.3-14ubuntu1.2
################################################################################
################################################################################
# Default Configuration
################################################################################
# Daemon name
daemonName="nordcloud-watchdog"
# PID settings
pidDir="./pid"
pidFile="${pidDir}/${daemonName}.pid"
# LOG settings
logDir="./log"
logFile="${logDir}/${daemonName}-"$(date +'%Y-%m-%d')".log"
logMaxSize=1024 # In Kbytes
# TMP settings
tmpDir="./tmp"
# Config directory settings for services
configDir="./conf.d"
# Daemon running interval
runInterval=60 # In seconds
################################################################################
# Static variables
################################################################################
# Config file location
configFile="./ncwd.conf"
# Scheduler location
schedulerDir="./lib"
schedulerFile="./lib/scheduler.sh"
# DOCS location
docsDir="./docs"
# Colors
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
WHITE="\e[39m"
################################################################################
# Do not edit below
################################################################################
# Reading config from file to overwrite defaults
source ${configFile}
# Reading scheduler and other functions
source ${schedulerFile}
myPid=$(echo $$)
doCommands() {
scheduler
}
setupDaemon() {
# Make sure that the directories work.
if [[ ! -d "${configDir}" ]]; then
mkdir -p "${configDir}"
fi
if [[ ! -d "${pidDir}" ]]; then
mkdir -p "${pidDir}"
fi
if [[ ! -d "${logDir}" ]]; then
mkdir "${logDir}"
fi
if [[ ! -f "${logFile}" ]]; then
touch "${logFile}"
else
# Check to see if we need to rotate the logs.
size=$((`ls -l "${logFile}" | cut -d " " -f 8`/1024))
if [[ ${size} -gt ${logMaxSize} ]]; then
mv ${logFile} "${logFile}.old"
touch "${logFile}"
fi
fi
# Checking for valid configuration only
if [[ $(configCheck) -eq 1 ]]; then
echo -e "\n${RED}Configuration error${WHITE}: please check service configurations.\n"
exit 1
fi
# Create running config to the tmp folder from the conf.d service files
initConfig
}
startDaemon() {
# Start the daemon.
setupDaemon # Make sure the directories are there.
if [[ $(checkDaemon) -eq 1 ]]; then
echo -e " * ${RED}Error${WHITE}: ${daemonName} is already running."
exit 1
fi
echo -e " * ${YELLOW}Starting${WHITE} ${daemonName} with PID: ${myPid}."
echo -e "${myPid}" > "${pidFile}"
log "*** $(date +'%Y-%m-%d'): Starting up ${daemonName}."
# Start the loop.
loop
}
stopDaemon() {
# Stop the daemon.
if [[ $(checkDaemon) -eq 0 ]]; then
echo -e " * ${RED}Error${WHITE}: ${daemonName} is not running."
exit 1
fi
echo -e " * ${YELLOW}Stopping ${daemonName} ${WHITE}"
log "*** $(date +'%Y-%m-%d'): ${daemonName} stopped."
if [[ ! -z $(cat ${pidFile}) ]]; then
kill -9 $(cat ${pidFile}) &> /dev/null
fi
rm -rf ${tmpDir}/* --preserve-root
}
statusDaemon() {
# Query and return whether the daemon is running.
if [[ $(checkDaemon) -eq 1 ]]; then
echo -e "\n ${GREEN}* ${daemonName} is running.${WHITE} \n"
else
echo -e "\n ${RED}* ${daemonName} isn't running.${WHITE} \n"
fi
exit 0
}
restartDaemon() {
# Restart the daemon.
if [[ $(checkDaemon) -eq 0 ]]; then
# Can't restart it if it isn't running.
echo -e " ${RED}${daemonName} isn't running.${WHITE}"
exit 1
fi
stopDaemon
startDaemon
}
checkDaemon() {
# Check to see if the daemon is running.
if [[ -z "${oldPid}" ]]; then
return 0
elif [[ $(ps aux | grep "${oldPid}" | grep -v grep) > /dev/null ]]; then
if [[ -f "${pidFile}" ]]; then
if [[ $(cat "${pidFile}") == "${oldPid}" ]]; then
# Daemon is running.
return 1
else
# Daemon isn't running.
return 0
fi
fi
elif [[ $(ps aux | grep "$daemonName" | grep -v grep | grep -v "$myPid" | grep -v "0:00.00") > /dev/null ]]; then
# Daemon is running but without the correct PID. Restart it.
log "*** $(date +'%Y-%m-%d'): ${daemonName} running with invalid PID; restarting."
restartDaemon
return 1
else
# Daemon not running.
return 0
fi
return 1
}
configCheck() {
# TODO: I would parse the service definitions for executables, and validate
# them against what exists currently. If a check cannot be performed I would
# drop an error message (for example curl for http checks) and then exit
# the daemon
log "*** $(date +'%Y-%m-%d'): Checking config"
}
addService() {
# TODO: Basic READ functions whith the given parameters like:
# service name, service description, check type (http, pid, socket, tcp)
# check period (s), restart action, restart numbers, restart interval
# notification email
log "*** $(date +'%Y-%m-%d'): $1 Service added"
}
removeService() {
# TODO: Basic READ functions for an existing service. Delete the file from
# the conf.d directory after that
log "*** $(date +'%Y-%m-%d'): $1 Service removed"
}
usage() {
# TODO: cat the content of the docs.txt file in the docs folder. Second param
# could be a specific command, and cat the specific file from there
cat ${docsDir}/usage.txt
}
loop() {
# This is the loop.
now=$(date +%s)
if [[ -z ${last} ]]; then
last=$(date +%s)
fi
# Do everything you need the daemon to do.
doCommands
# Check to see how long we actually need to sleep for. If we want this to run
# once a minute and it's taken more than a minute, then we should just run it
# anyway.
last=$(date +%s)
# Set the sleep interval
if [[ ! $((now-last+runInterval+1)) -lt $((runInterval)) ]]; then
sleep $((now-last+runInterval))
fi
# Startover
loop
}
initConfig() {
# TODO: Create a config file in tmp with restructuring the service files
# from conf.d folder. Line by line a single service and it's full definitions
# this will be parsed by the scheduler
log "*** $(date +'%Y-%m-%d'): Initializing config"
}
log() {
# Generic log function.
echo "$1" >> "$logFile"
}
################################################################################
# Main
################################################################################
if [[ -f "${pidFile}" ]]; then
oldPid=$(cat "${pidFile}")
fi
checkDaemon
case "$1" in
start)
startDaemon
;;
stop)
stopDaemon
;;
status)
statusDaemon
;;
restart)
restartDaemon
;;
usage)
usage
;;
configcheck)
configCheck
;;
add-service)
addService
;;
remove-service)
removeService
;;
*)
echo -e "\n${RED}Error${WHITE}: usage $0 { start | stop | restart | status | configcheck | add-service | remove-service | usage }\n"
exit 1
esac
exit 0