izmjene licence
[ossec-hids.git] / src / init / ossec-client.sh
1 #!/bin/sh
2 # ossec-control        This shell script takes care of starting
3 #                      or stopping ossec-hids
4 # Author: Daniel B. Cid <daniel.cid@gmail.com>
5
6
7 LOCAL=`dirname $0`;
8 cd ${LOCAL}
9 PWD=`pwd`
10 DIR=`dirname $PWD`;
11
12 ###  Do not modify bellow here ###
13 NAME="OSSEC HIDS"
14 VERSION="v2.8.3"
15 AUTHOR="Trend Micro Inc."
16 DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd ossec-execd"
17
18
19 ## Locking for the start/stop
20 LOCK="${DIR}/var/start-script-lock"
21 LOCK_PID="${LOCK}/pid"
22
23
24 # This number should be more than enough (even if it is
25 # started multiple times together). It will try for up
26 # to 10 attempts (or 10 seconds) to execute.
27 MAX_ITERATION="10"
28
29
30
31 # Check pid
32 checkpid()
33 {
34     for i in ${DAEMONS}; do
35         for j in `cat ${DIR}/var/run/${i}*.pid 2>/dev/null`; do
36             ps -p $j |grep ossec >/dev/null 2>&1
37             if [ ! $? = 0 ]; then
38                 echo "Deleting PID file '${DIR}/var/run/${i}-${j}.pid' not used..."
39                 rm ${DIR}/var/run/${i}-${j}.pid
40             fi    
41         done    
42     done    
43 }
44
45
46
47 # Lock function
48 lock()
49 {
50     i=0;
51     
52     # Providing a lock.
53     while [ 1 ]; do
54         mkdir ${LOCK} > /dev/null 2>&1
55         MSL=$?
56         if [ "${MSL}" = "0" ]; then
57             # Lock aquired (setting the pid)
58             echo "$$" > ${LOCK_PID}
59             return;
60         fi
61
62         # Waiting 1 second before trying again
63         sleep 1;
64         i=`expr $i + 1`;
65
66         # If PID is not present, speed things a bit.
67         kill -0 `cat ${LOCK_PID}` >/dev/null 2>&1
68         if [ ! $? = 0 ]; then
69             # Pid is not present.
70             i=`expr $i + 1`;
71         fi    
72
73         # We tried 10 times to acquire the lock.
74         if [ "$i" = "${MAX_ITERATION}" ]; then
75             # Unlocking and executing
76             unlock;
77             mkdir ${LOCK} > /dev/null 2>&1
78             echo "$$" > ${LOCK_PID}
79             return;
80         fi
81     done
82 }
83
84
85 # Unlock function
86 unlock()
87 {
88     rm -rf ${LOCK}
89 }
90
91     
92 # Help message
93 help()
94 {
95     # Help message
96     echo "Usage: $0 {start|stop|restart|status}";
97     exit 1;
98 }
99
100
101 # Status function
102 status()
103 {
104     RETVAL=0
105     for i in ${DAEMONS}; do
106         pstatus ${i};
107         if [ $? = 0 ]; then
108             RETVAL=1
109             echo "${i} not running..."
110         else
111             echo "${i} is running..."
112         fi
113     done             
114     exit $RETVAL
115 }
116
117 testconfig()
118 {
119     # We first loop to check the config. 
120     for i in ${SDAEMONS}; do
121         ${DIR}/bin/${i} -t;
122         if [ $? != 0 ]; then
123             echo "${i}: Configuration error. Exiting"
124             unlock;
125             exit 1;
126         fi    
127     done
128 }
129
130 # Start function
131 start()
132 {
133     SDAEMONS="ossec-execd ossec-agentd ossec-logcollector ossec-syscheckd"
134     
135     echo "Starting $NAME $VERSION (by $AUTHOR)..."
136     lock;
137     checkpid;
138
139     
140     # We actually start them now.
141     for i in ${SDAEMONS}; do
142         pstatus ${i};
143         if [ $? = 0 ]; then
144             ${DIR}/bin/${i};
145             if [ $? != 0 ]; then
146                 echo "${i} did not start";
147                 unlock;
148                 exit 1;
149             fi 
150
151             echo "Started ${i}..."            
152         else
153             echo "${i} already running..."                
154         fi    
155     
156     done    
157
158     # After we start we give 2 seconds for the daemons
159     # to internally create their PID files.
160     sleep 2;
161     unlock;
162     echo "Completed."
163 }
164
165 # Process status
166 pstatus()
167 {
168     pfile=$1;
169     
170     # pfile must be set
171     if [ "X${pfile}" = "X" ]; then
172         return 0;
173     fi
174         
175     ls ${DIR}/var/run/${pfile}*.pid > /dev/null 2>&1
176     if [ $? = 0 ]; then
177         for j in `cat ${DIR}/var/run/${pfile}*.pid 2>/dev/null`; do
178             ps -p $j |grep ossec >/dev/null 2>&1
179             if [ ! $? = 0 ]; then
180                 echo "${pfile}: Process $j not used by ossec, removing .."
181                 rm -f ${DIR}/var/run/${pfile}-$j.pid
182                 continue;
183             fi
184                 
185             kill -0 $j > /dev/null 2>&1
186             if [ $? = 0 ]; then
187                 return 1;
188             fi    
189         done    
190     fi
191     
192     return 0;    
193 }
194
195
196 # Stop all
197 stopa()
198 {
199     lock;
200     checkpid;
201     for i in ${DAEMONS}; do
202         pstatus ${i};
203         if [ $? = 1 ]; then
204             echo "Killing ${i} .. ";
205             
206             kill `cat ${DIR}/var/run/${i}*.pid`;
207         else
208             echo "${i} not running .."; 
209         fi
210         
211         rm -f ${DIR}/var/run/${i}*.pid
212         
213      done    
214     
215     unlock;
216     echo "$NAME $VERSION Stopped"
217 }
218
219
220 ### MAIN HERE ###
221
222 case "$1" in
223   start)
224     testconfig
225         start
226         ;;
227   stop) 
228         stopa
229         ;;
230   restart)
231     testconfig
232         stopa
233         sleep 1;
234         start
235         ;;
236   reload)
237         DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd"
238         stopa
239         start
240         ;;
241   status)
242     status
243         ;;
244   help)  
245     help
246     ;;
247   *)
248     help
249 esac
250