Privacy Policy
Snippets index

  Monit sample configuration file

file "/etc/monitrc":

set daemon 120
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
set eventqueue
      basedir /var/lib/monit/events # set the base directory where events will be stored
      slots 100                     # optionally limit the queue size

set httpd port 2812 and
    #use address 0.0.0.0    # only accept connection from localhost
    allow localhost
    allow 0.0.0.0/0.0.0.0
    allow 192.168.98.10
    allow admin:monit       # require user 'admin' with password 'monit'

check process daemon with pidfile /home/alfadesk/run/daemon.pid
    stop program  = "/bin/bash -c '/bin/kill -9 `cat /home/alfadesk/run/daemon.pid`'"
    if memory usage > 100 MB for 1 cycles then stop

Sample usage

root@user-BW551:/etc# monit
Starting Monit 5.25.1 daemon with http interface at [*]:2812

root@user-BW551:/etc# monit status
Monit 5.25.1 uptime: 1m

Process 'daemon'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          2522
  parent pid                   1322
  uid                          1002
  effective uid                1002
  gid                          1003
  uptime                       15h 23m
  threads                      1
  children                     0
  cpu                          10.6%
  cpu total                    10.6%
  memory                       2.6% [49.4 MB]
  memory total                 2.6% [49.4 MB]
  security attribute           unconfined
  disk write                   0 B/s [4 kB total]
  data collected               Wed, 29 Nov 2017 10:13:21

System 'user-BW551'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.17] [0.29] [0.27]
  cpu                          3.6%us 1.4%sy 0.1%wa
  memory usage                 296.0 MB [15.9%]
  swap usage                   464 kB [0.0%]
  uptime                       5d 17h 57m
  boot time                    Thu, 23 Nov 2017 16:15:56
  data collected               Wed, 29 Nov 2017 10:13:21

root@user-BW551:/etc# monit monitor all
root@user-BW551:/etc#

root@user-BW551:/etc# monit quit
Monit daemon with pid [9157] killed

Check with Python

>>> import psutil
>>> [process.pid for i in psutil.process_iter()]
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 44, 45, 46, 47, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 79, 92, 93, 142, 143, 144, 145, 146, 151, 162, 163, 284, 285, 286, 287, 305, 309, 401, 411, 475, 507, 532, 707, 710, 742, 941, 947, 948, 964, 965, 969, 1016, 1022, 1027, 1030, 1040, 1055, 1070, 1096, 1180, 1212, 1216, 1222, 1232, 1233, 1236, 1240, 1243, 1247, 1251, 1269, 1288, 1322, 1366, 2513, 2515, 2516, 2518, 2522, 2562, 4792, 5769, 5770, 5771, 5772, 5773, 5791, 5797, 5798, 5873, 5894, 6186, 8890, 9106, 9118, 9122, 9123, 9134, 9135, 9136, 9147, 9183, 9261, 9268, 9289, 9291, 11052, 13632, 13634, 13635, 13636, 13637, 13638, 16334, 18658]
>>> [process.pid for i in psutil.process_iter() if process.pid==2522]
[2522]
>>> [i for i in psutil.process_iter() if process.pid==2522]
[<psutil.Process(pid=2522, name='python') at 139966775253328>]
>>> [process.memory_info().rss for process in psutil.process_iter() if process.pid==2522]
[51806208]
>>>

From bash:

cat /proc/2522/status

(python)root@user-BW551:/home/alfadesk/alfadesk#  cat /proc/2522/status
Name:   python
State:  R (running)
Tgid:   2522
Ngid:   0
Pid:    2522
PPid:   1322
TracerPid:  0
Uid:    1002    1002    1002    1002
Gid:    1003    1003    1003    1003
FDSize: 64
Groups: 20 1003
NStgid: 2522
NSpid:  2522
NSpgid: 2522
NSsid:  1322
VmPeak:   168100 kB
VmSize:   168100 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     50592 kB
VmRSS:     50592 kB
VmData:    40712 kB
VmStk:       132 kB
VmExe:      2796 kB
VmLib:     12688 kB
VmPTE:       336 kB
VmPMD:        12 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
Threads:    1
SigQ:   0/7213
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000001001000
SigCgt: 0000000180000002
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
Seccomp:    0
Cpus_allowed:   f
Cpus_allowed_list:  0-3
Mems_allowed:   00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    3913663
nonvoluntary_ctxt_switches: 70187

Kill a process from Python

p = psutil.Process(pid)
p.terminate()  #or p.kill()

https://stackoverflow.com/questions/17856928/how-to-terminate-process-from-python-using-pid#17858114