This file is indexed.

/usr/lib/ocf/resource.d/heartbeat/kamailio is in resource-agents 1:4.1.0~rc1-1ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#!/bin/bash
#
#       OCF resource agent for Kamailio for pacemaker
#

# Copyright (c) 2013 FREQUENTIS AG,
#                    Authors: Stefan Wenk
#                             Rainer Brestan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

# OCF input parameters:
#  OCF_RESKEY_binary
#  OCF_RESKEY_conffile
#  OCF_RESKEY_pidfile
#  OCF_RESKEY_monitoring_ip
#  OCF_RESKEY_listen_address
#  OCF_RESKEY_port
#  OCF_RESKEY_proto
#  OCF_RESKEY_sipsak
#  OCF_RESKEY_kamctl
#  OCF_RESKEY_kamctlrc
#  OCF_RESKEY_kamuser
#  OCF_RESKEY_kamgroup
#  OCF_RESKEY_extra_options

# Initialization:

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

#######################################################################
# Defaults

RESKEY_binary_default="/usr/sbin/kamailio"
RESKEY_conffile_default="/etc/kamailio/kamailio.cfg"
RESKEY_pidfile_default="/var/run/kamailio_${OCF_RESOURCE_INSTANCE}/kamailio.pid"
RESKEY_monitoring_ip_default=127.0.0.1
RESKEY_port_default=5060
RESKEY_proto_default="udptcp"
RESKEY_sipsak_default="/usr/bin/sipsak"
RESKEY_kamctl_default="/usr/bin/kamctl"
RESKEY_kamctlrc_default="/etc/kamailio/kamctlrc"
RESKEY_kamuser_default=""
RESKEY_kamgroup_default=""
RESKEY_extra_options_default=""

#######################################################################
: ${OCF_RESKEY_binary=${RESKEY_binary_default}}
: ${OCF_RESKEY_conffile=${RESKEY_conffile_default}}
: ${OCF_RESKEY_pidfile=${RESKEY_pidfile_default}}
: ${OCF_RESKEY_monitoring_ip=${RESKEY_monitoring_ip_default}}
: ${OCF_RESKEY_port=${RESKEY_port_default}}
: ${OCF_RESKEY_proto=${RESKEY_proto_default}}
: ${OCF_RESKEY_sipsak=${RESKEY_sipsak_default}}
: ${OCF_RESKEY_kamctl=${RESKEY_kamctl_default}}
: ${OCF_RESKEY_kamctlrc=${RESKEY_kamctlrc_default}}
: ${OCF_RESKEY_kamuser=${RESKEY_kamuser_default}}
: ${OCF_RESKEY_kamgroup=${RESKEY_kamgroup_default}}
: ${OCF_RESKEY_extra_options=${RESKEY_extra_options_default}}

#######################################################################
usage() {
  cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}

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

<longdesc lang="en">
 Resource agent for the Kamailio SIP proxy/registrar.
 Multiple instances are possible when using following parameter combinations:

 Parameters for Kamailio instance 1:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=udptcp
    port=5060 

 Parameters for Kamailio instance 2:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=udp
    port=5070
    conffile=/etc/kamailio/kamailio2.cfg
    kamctlrc=""

 Only one instance can be monitored via the command "kamctl monitor"
 because the kamctl tool of kamailio 4.x is not designed for multiple
 instances. Therefore, the provided kamctrlrc file path needs to be 
 empty for instance 2, 3 ...

Parameters for a third Kamailio instance:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=tcp
    port=5080
    conffile=/etc/kamailio/kamailio3.cfg
    kamctlrc=""
</longdesc>

<shortdesc lang="en">Resource agent for Kamailio</shortdesc>

<parameters>
  <parameter name="binary" unique="0" required="0">
   <longdesc lang="en">The kamailio binary</longdesc>
   <shortdesc lang="en">The kamailio  binary</shortdesc>
   <content type="string" default="${RESKEY_binary_default}" />
  </parameter>

  <parameter name="conffile" unique="0" required="0">
   <longdesc lang="en">
    The kamailio configuration file name with full path.
    For example, "/etc/kamailio/kamailio.cfg" , which is the default value.
    Make sure to use unique names in case of having multiple instances.
   </longdesc>
   <shortdesc lang="en">Configuration file name with full path</shortdesc>
   <content type="string" default="${RESKEY_conffile_default}" />
  </parameter>

  <parameter name="pidfile" unique="0" required="0">
   <longdesc lang="en">
     The kamailio PID file. The directory used must be writable by kamailio
     process user. Be sure to use unique name for running more than one
     instance. Try to use absolute path names.
     If empty, resource agent create a unique directory from the resource
     instance name for the PID file and assign it to the process user.
   </longdesc>
   <shortdesc lang="en">PID file</shortdesc>
   <content type="string" default="${RESKEY_pidfile_default}" />
  </parameter>

  <parameter name="monitoring_ip" unique="0" required="0">
  <longdesc lang="en">
   SIP IP Address of the kamailio instance used for SIP OPTIONS polling monitoring.
   Usually the same IP address value as for parameter listen_address should be
   provided.

   In order to respond with a 200 OK response to the SIP OOPTION requests, 
   the kamailio.cfg file needs to contain following section:
   Note: The following "kamailio.cfg" code sniplet is part of an XML section. 
         Therefore it contains two &amp; characters, which need to be replaced 
         with two ampersand characters within "kamailio.cfg":

   if (is_method("OPTIONS") &amp;&amp; ($ru=~"sip:monitor@.*")) {
        ##
        ## If the method is an OPTIONS we are simply going to respond
        ## with a 200 OK.  
        # xlog("L_INFO", "Method is an OPTIONS, probably just monitoring\n");   
        sl_send_reply("200", "Kamailio is alive");
        exit;
    }

  </longdesc>
  <shortdesc lang="en">Monitoring IP address used for SIP OPTIONS polling.</shortdesc>
   <content type="string" default="${RESKEY_monitoring_ip_default}" />
  </parameter>

  <parameter name="listen_address" unique="0" required="1">
  <longdesc lang="en">
    SIP IP address the kamailio will listen on.
  </longdesc>
  <shortdesc lang="en">Listening SIP address</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="port" unique="0" required="0">
   <longdesc lang="en">
    SIP port for the kamailio instance.
   </longdesc>
   <shortdesc lang="en">SIP Port</shortdesc>
   <content type="string" default="${RESKEY_port_default}" />
  </parameter>

  <parameter name="extra_options" unique="0" required="0">
   <longdesc lang="en">
    Extra options to add to kamailio start.
   </longdesc>
   <shortdesc lang="en">extra_options</shortdesc>
   <content type="string" default="${RESKEY_extra_options}" />
  </parameter>


  <parameter name="proto" unique="0" required="0">
   <longdesc lang="en">
    The protocol used for SIP proto  =  udp|tcp|udptcp|conf_udp|conf_tcp|conf_udptcp.
    Using the options "conf_*" does not add any "-l" parameters to the kamailio command,
    the "listen" parameters from kamailio.conf are used instead. The sipsak checks are
    performed depending what protocol is defined after the underscore.
   </longdesc>
   <shortdesc lang="en">protocol</shortdesc>
   <content type="string" default="${RESKEY_proto_default}" />
  </parameter>

  <parameter name="sipsak" unique="0" required="0">
   <longdesc lang="en">
    The installation path of the sipsak tool, which is used 
    for monitoring Kamailio via SIP OPTIONS polling. 
   </longdesc>
   <shortdesc lang="en">sipsak path</shortdesc>
   <content type="string" default="${RESKEY_sipsak_default}" />
  </parameter>

  <parameter name="kamctl" unique="0" required="0">
   <longdesc lang="en">
    The installation path of the "kamctl" control tool. 
   </longdesc>
   <shortdesc lang="en">kamctl path</shortdesc>
   <content type="string" default="${RESKEY_kamctl_default}" />
  </parameter>

  <parameter name="kamctlrc" unique="0" required="0">
   <longdesc lang="en">
    The location of the "kamctlrc" file for the Kamailio instance.
    The file "kamctlrc" is the Kamailio configuration file for its "kamctl" control tool.

    This parameter only needs to be provided in case of using multiple Kamailio server
    instances on a single cluster node:

    In case that the parameter "kamctlrc" is not empty, this ressource agent monitors 
    the health state of the Kamailio server via the command "kamctl monitor 1". This 
    setting is recommended in case of using a single Kamailio server instance.

    In case that the parameter "kamctlrc" is empty, the ressource agent does not 
    monitor the health state of the Kamailio server instance via the "kamctl" command.

    Please note that the "kamctl" control command of Kamailio 4.x does not support 
    running multiple Kamailio instances on one host. Nevertheless this resource agent 
    does allow multiple Kamailio instances per host. The result of the "kamctl" 
    limitation in terms of number of Kamailio server instances is that the health 
    check via "kamctl monitor 1" can be configured for a single Kamailio instance
    only.

    Please refer to the long description of this resoure agent for an example
    of parameter combinations in case that multiple instances are to be 
    configured per cluster node.

   </longdesc>
   <shortdesc lang="en">kamctlrc path</shortdesc>
   <content type="string" default="${RESKEY_kamctlrc_default}" />
  </parameter>

  <parameter name="kamuser" unique="0" required="0">
   <longdesc lang="en">
    The user account for kamailio process to run with.
    Uses the current user, if not specified or empty.
    There is no check, if running kamailio with the specified user account is possible.
   </longdesc>
   <shortdesc lang="en">kamailio user</shortdesc>
   <content type="string" default="${RESKEY_kamuser_default}" />
  </parameter>

  <parameter name="kamgroup" unique="0" required="0">
   <longdesc lang="en">
    The group for kamailio process to run with.
    Uses the current group, if not specified or empty.
   </longdesc>
   <shortdesc lang="en">kamailio group</shortdesc>
   <content type="string" default="${RESKEY_kamgroup_default}" />
  </parameter>
</parameters>

 <actions>
  <action name="start" timeout="60" />
  <action name="stop" timeout="30" />
  <action name="status" timeout="30" interval="10" />
  <action name="monitor" timeout="30" interval="10" />
  <action name="meta-data" timeout="5" />
  <action name="validate-all" timeout="5" />
  <action name="notify" timeout="5" />
 </actions>
</resource-agent>
END

    exit $OCF_SUCCESS
}

#######################################################################

###
#Check if a process with given PID is running
# Parameter 1: PID
###
isRunning_PID()
{
    kill -s 0 "$1" > /dev/null 2>&1
}

###
#Check if an instance with given command line is running
# Parameter 1: command line.
###
isRunning_cmd()
{
    pkill -s 0 "$1" > /dev/null 2>&1
}

###
# Formats the result of a command.
#
# Parameter 1: Exit status.
# Parameter 2: Standard output (stdout).
# Parameter 3: Error output (stderr).
#     Returns: Formatted result.
kamailio_format_result() {
    local exitstatus="$1"
    local value="$2"
    local error="$3"

    echo -n "exit status: ${exitstatus}"
    if [ -n "$value" ]; then
        echo -n ", value: ${value}"
    fi

    if [ -n "$error" ]; then
        echo -n ", error: ${error}"
    fi
    echo
}

###
# Put the command line, how the kamailio process is started according 
# to the configured parameters, into the variable "kam_cmd".
###

kamailio_cmd()
{
    case ${OCF_RESKEY_proto} in
    udp)  listen_param="-T -l udp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l udp:127.0.0.1:${OCF_RESKEY_port}"
           ;;
    tcp)  listen_param="-l tcp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l tcp:127.0.0.1:${OCF_RESKEY_port}"
           ;;
    udptcp)  listen_param1="-l udp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l udp:127.0.0.1:${OCF_RESKEY_port}"
              listen_param2="-l tcp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l tcp:127.0.0.1:${OCF_RESKEY_port}"
              listen_param="${listen_param1} ${listen_param2}"
           ;;
    conf_*)
           # doing nothing, no listen_param set
           ;;
    *)  listen_param="-T"
           ;;
    esac

    kam_cmd="${OCF_RESKEY_binary} -P ${OCF_RESKEY_pidfile} -f ${OCF_RESKEY_conffile}"

    if [ -n "${listen_param}" ]; then
        kam_cmd="${kam_cmd} ${listen_param}"
    fi
    if [ -n "${OCF_RESKEY_kamuser}" ]; then
        kam_cmd="${kam_cmd} -u ${OCF_RESKEY_kamuser}"
    fi
    if [ -n "${OCF_RESKEY_kamgroup}" ]; then
        kam_cmd="${kam_cmd} -g ${OCF_RESKEY_kamgroup}"
    fi
    if [ -n "${OCF_RESKEY_extra_options}" ]; then
        kam_cmd="${kam_cmd} ${OCF_RESKEY_extra_options}"
    fi
}

###
# Gets the PID for the running Kamailio instance.
#
#     Returns: The variable $PID contains the found PID value or an empty string.
# Exit Status: Zero if the PID file was found and this process run under
#              the command line parameters of our instance.
#              1) if the PID file is not present and no process running under
#                 our command line options is active.
#              2) in all other fatal cases, which we classify in the followig 
#                 as OCF_ERR_genering. These are folloing cases:
#                a) The PID file contains a PID value which does no match to 
#                   to our instance
#                b) The PID contains a empty string in its first line
#                c) The PID file contains some text and some processeses
#                   from our instance are still active

kamailio_get_pid() {
    if [ -f ${OCF_RESKEY_pidfile} ]; then
        PID=`head -n 1 $OCF_RESKEY_pidfile`
        if [ ! -z "$PID" ]; then
            #Cross check if the PID file really contains a process of our kamailio instance:
            kamailio_cmd
            CROSSPID=`pgrep -o -f "${kam_cmd}"`
            if [ x"$PID" == x"$CROSSPID" ]; then
                #ocf_log debug "Found kamailio process PID with value: $PID."
                return 0
            fi
            #ocf_log debug "PID file does not contain a PID of a $OCF_RESKEY_binary process!"
            return 2
        fi

        #PID file does not contain a valid PID
        rm -f ${OCF_RESKEY_pidfile}
        return 2
    fi

    # No PID file found!
    # Check if still a process exists even though we don't have the PID any longer:
    kamailio_cmd
    pgrep -f "${kam_cmd}"
    if [ $? -eq 0 ]; then
        ocf_log info "PID file does not contain a valid PID, but kamailio process is still active"
        return 2
    fi

    ocf_log info "No PID file found and our kamailio instance is not active"
    return 1
}

kamailio_status() {
    local not_running_log_level="warn"
    local errorfile error output

    if [ "$__OCF_ACTION" = "start" ]; then
        not_running_log_level="debug"
    fi

    kamailio_get_pid >/dev/null
    RET=$?
    if [ $RET -ne 0 ]; then
        if [ $RET -eq 2 ]; then
            ocf_log $not_running_log_level "PID file does not contain a PID of a ${OCF_RESKEY_binary} process!"
            return $OCF_ERR_GENERIC
        fi
        return $OCF_NOT_RUNNING
    fi

    PID=`head -n 1 $OCF_RESKEY_pidfile`
    isRunning_PID "$PID"
    RET=$?
    if [ "$RET" -ne 0 ]; then
        ocf_log $not_running_log_level "PID from $PID from ${OCF_RESKEY_pidfile} not running"
        rm -f ${OCF_RESKEY_pidfile}
        return $OCF_NOT_RUNNING
    fi

    rc=0
    # In case that OCF_RESKEY_kamctlrc we perfom a health check via "kamctl monitor 1"
    if [ ! -z ${OCF_RESKEY_kamctlrc} ]; then
        # PID is running now but it is not save to check via kamctl without care, because 
        # the implementation analysis in the case that we kill all running processes
        # shows that in case that the fifo cannot be read, then kamctl blocks. This needs
        # to be avoided.
        # In order to be on the safe side, we run this check therefore under "timeout" control:
        rc=1
        timeout 3 ${OCF_RESKEY_kamctl} monitor 1 |grep "since" ; rc=$?
    fi

    if [ $rc -ne 0 ]; then
        ocf_log $not_running_log_level "Kamailio is not up according to kamctl monitor!"
        return $OCF_NOT_RUNNING
    fi

    errorfile=`mktemp`
    case ${OCF_RESKEY_proto} in
    udp)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
          result=$?
          ;;
    tcp)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport tcp>/dev/null 2>>$errorfile`
          result=$?
          ;;
    udptcp) output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport tcp>/dev/null 2>>$errorfile`
            result=$?
            if [ $result -eq 0 ]; then
                output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
                result=$?
            fi
            ;;
    *)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
        result=$?
        ;;
    esac

    error=`cat $errorfile`
    rm -f $errorfile

    if [ $result -ne 0 ]; then
        ocf_log $not_running_log_level "Kamailio is running, but not functional as sipsak ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")"
        return $OCF_ERR_GENERIC
    fi

    return $OCF_SUCCESS
}

kamailio_monitor() {
    kamailio_status
}

kamailio_start() {
    local errorfile error output piddir

    if
        kamailio_status
    then
        ocf_log info "kamailio already running."
        return $OCF_SUCCESS
    fi

    # if pidfile directory does not exist, create it with kamailio process owner
    piddir=`dirname "${OCF_RESKEY_pidfile}"`
    if [ ! -d "$piddir" ]; then
        mkdir -p "$piddir"
        if [ "$OCF_RESKEY_kamuser" != "" ]; then
            chown ${OCF_RESKEY_kamuser} "$piddir"
        fi
    fi

    kamailio_cmd
    if [ "$OCF_RESKEY_kamuser" != "" ]; then
        kam_cmd="su -s /bin/bash $OCF_RESKEY_kamuser -c \"$kam_cmd\""
    fi

    ocf_log info "start kamailio with $kam_cmd."
    errorfile=`mktemp`
    output=$(eval ${kam_cmd} 2>>$errorfile)
    result=$?
    error=`cat $errorfile`
    rm -f $errorfile

    if [ $result -eq 0 ]; then
        result=1
        while [ $result -ne 0 ]; do
            sleep 1
            kamailio_get_pid >/dev/null
            result=$?
        done

        ocf_log info "kamailio instance PID=$PID started."
        # check with monitor operation if running correctly
        result=$OCF_ERR_GENERIC
        while [ $result -ne $OCF_SUCCESS ]; do
            sleep 1
            kamailio_monitor
            result=$?
            ocf_log info "monitor in start returned $result"
        done
        ocf_log info "kamailio started successful."

    else
        ocf_log err "kamailio instance could not be started, $(kamailio_format_result $result "$output" "$error")"
        result=$OCF_ERR_GENERIC
    fi

    return $result
}

kamailio_stop() {
    local piddir
    local TRIES=0
    result=$OCF_SUCCESS

    kamailio_cmd

    ocf_log info "Stopping kamailio by sending SIGTERM to ${kam_cmd}"
    pkill -SIGTERM -x -f "${kam_cmd}"
    if [ $? -eq 1 ]; then
        # already stopped. no processes found
        # in case of not specified pidfile, delete the created directory
        # otherwise only the pidfile itself
        if [ "${OCF_RESKEY_pidfile}" == "${RESKEY_pidfile_default}" ]; then
            piddir=`dirname "${OCF_RESKEY_pidfile}"`
            rm -rf "$piddir"
        else
            rm -f "${OCF_RESKEY_pidfile}"
        fi
        return $result
    fi

    if [ "$OCF_RESKEY_CRM_meta_timeout" != "" ]; then
        KAMAILIO_STOP_TIMEOUT=$(( ($OCF_RESKEY_CRM_meta_timeout/1000) - 7 ))
    else
        KAMAILIO_STOP_TIMEOUT=20
    fi

    while isRunning_cmd "${kam_cmd}" && [ "$TRIES" -lt "${KAMAILIO_STOP_TIMEOUT}" ]
    do
        sleep 1
        ocf_log info "kamailio ${kam_cmd} is still running after SIGTERM"
        ((TRIES++))
    done

    isRunning_cmd "${kam_cmd}"
    RET=$?

    if [ "$RET" -eq 0 ]; then
        ocf_log info "Killing ${kam_cmd} with SIGKILL"
        TRIES=0
        pkill -SIGKILL -x -f "${kam_cmd}" > /dev/null 2>&1

        while isRunning_cmd "${kam_cmd}" && [ "$TRIES" -lt 3 ]
        do
            sleep 1
            ocf_log info "kamailio ${kam_cmd} is still running after SIGKILL"
            ((TRIES++))
        done

        isRunning_cmd "${kam_cmd}"
        RET=$?
        if [ "$RET" -eq 0 ]; then
            ocf_log fatal "kamailio is still running even after SIGKILL"
            result=$OCF_ERR_GENERIC
        fi
    else
        ocf_log info "${kam_cmd} has stopped."
    fi

    # in case of not specified pidfile, delete the created directory
    # otherwise only the pidfile itself
    if [ "${OCF_RESKEY_pidfile}" == "${RESKEY_pidfile_default}" ]; then
        piddir=`dirname "${OCF_RESKEY_pidfile}"`
        rm -rf "$piddir"
    else
        rm -f "${OCF_RESKEY_pidfile}"
    fi
    return $result

}

kamailio_validate_all() {
    # Check if kamailio configuration is valid before starting the server

    if [ ! -f $OCF_RESKEY_binary ]; then
       ocf_log err "File OCF_RESKEY_binary [${OCF_RESKEY_binary}] does not exist!"
       return $OCF_NOT_INSTALLED
    fi
  
    out=$($OCF_RESKEY_binary -c 2>&1 > /dev/null)
    retcode=$?
    if [ "$retcode" -ne '0' ]; then
        ocf_log info "Not starting kamailio: $OCF_RESKEY_binary does not start!"
        return $OCF_ERR_CONFIGURED
    fi

    case $OCF_RESKEY_monitoring_ip in
    "")    ocf_log err "Required parameter OCF_RESKEY_monitoring_ip is missing!"
           return $OCF_ERR_CONFIGURED
           ;;
    [0-9]*.[0-9]*.[0-9]*.[0-9]*)     : OK
            ;;
    *)     ocf_log err "Parameter OCF_RESKEY_monitoring_ip [$OCF_RESKEY_monitoring_ip] is not an IP address!"
           return $OCF_ERR_CONFIGURED
           ;;
    esac 

    case $OCF_RESKEY_listen_address in
    "")    ocf_log err "Required parameter $OCF_RESKEY_listen_address is missing!"
           return $OCF_ERR_CONFIGURED
           ;;
    [0-9]*.[0-9]*.[0-9]*.[0-9]*)    : OK
           ;;
    *)     ocf_log err "Parameter OCF_RESKEY_listen_address [$OCF_RESKEY_listen_address] not an IP address!"
           return $OCF_ERR_CONFIGURED
           ;;
    esac 

    if [ ! -f ${OCF_RESKEY_sipsak} ]; then
        ocf_log err "sipsak [${OCF_RESKEY_sipsak}] does not exist!"
        return $OCF_NOT_INSTALLED
    fi

    if [ ! -z ${OCF_RESKEY_kamctlrc} ]; then
        if [ ! -f ${OCF_RESKEY_kamctlrc} ]; then
            ocf_log err "kamctlrc file [${kamctlrc}] does not exist!"
            return $OCF_NOT_INSTALLED
        fi
    else
        ocf_log debug "No monitoring via kamctl monitor because the parameter [kamctlrc] is empty."
    fi

    if [ ! -f ${OCF_RESKEY_conffile} ]; then
        ocf_log err "Kamailio configuration file provided in the parameter conffile [${OCF_RESKEY_conffile}] does not exist!"
        return $OCF_ERR_CONFIGURED
    fi

    case $OCF_RESKEY_proto in
    "")   ocf_log err "Parameter $OCF_RESKEY_proto is empty!"
          return $OCF_ERR_CONFIGURED
          ;;
    udp|tcp|udptcp)    : OK
        ;;
    *)   ocf_log err "Parameter value $OCF_RESKEY_proto for parameter [proto] not yet supported!"
         return $OCF_ERR_CONFIGURED
         ;;
    esac

    return $OCF_SUCCESS
}

if [ $# -ne 1 ]; then
    usage
    exit $OCF_ERR_ARGS
fi

case $__OCF_ACTION in
    meta-data) meta_data
        exit $OCF_SUCCESS
    ;;
    start|stop|status|monitor)
        kamailio_${__OCF_ACTION}
    ;;
    validate-all) kamailio_validate_all
    ;;
    notify) exit $OCF_SUCCESS
    ;;
    usage) usage
        exit $OCF_SUCCESS
    ;;
#   reload) #Not supported by Kamailio, but not needed by pacemaker
#   ;;
#   recover #Not needed by pacemaker
#   ;;
    *) usage
        exit $OCF_ERR_UNIMPLEMENTED
        ;;
esac

exit $?