|
| 1 | +# DoMonit |
| 2 | + |
| 3 | +A deadly simple monitoring tool for Docker - Using A Python Wrapper For Docker API |
| 4 | + |
| 5 | +# Compatibility |
| 6 | + |
| 7 | +A Python wrapper for Dokcer API 1.24 compatible with Docker 1.12.x and later. |
| 8 | + |
| 9 | +# Purpose |
| 10 | + |
| 11 | +The purpose is to write python scripts easily for monitoring all of your Docker containers (running in a Linux distibution - other OS are coming soon in the roadmap of development). |
| 12 | + |
| 13 | +# The Wrapper |
| 14 | + |
| 15 | +The wrapper contains these classes: |
| 16 | + |
| 17 | +``` |
| 18 | +api/ |
| 19 | +├── changes.py |
| 20 | +├── containers.py |
| 21 | +├── errors.py |
| 22 | +├── ids.py |
| 23 | +├── inspect.py |
| 24 | +├── logs.py |
| 25 | +├── process.py |
| 26 | +└── stats.py |
| 27 | +``` |
| 28 | + |
| 29 | +Where : |
| 30 | + |
| 31 | +**Containers** : List containers |
| 32 | + |
| 33 | +**Inspect** : Return low-level information on the container id |
| 34 | + |
| 35 | +**Ids** : Return containers IDs |
| 36 | + |
| 37 | +**Logs** : Get stdout and stderr logs from the container id |
| 38 | + |
| 39 | +**Process** : List processes running inside the container id. On Unix systems this is done by running the ps command. This endpoint is not supported on Windows. |
| 40 | + |
| 41 | +**Stats** : This endpoint returns a live stream of a container’s resource usage statistics. |
| 42 | + |
| 43 | + |
| 44 | +# Example |
| 45 | + |
| 46 | +Create a virtual environement |
| 47 | +``` |
| 48 | +virtualenv domonit |
| 49 | +cd domonit |
| 50 | +. bin/activate |
| 51 | +git clone https://github.com/eon01/DoMonit.git |
| 52 | +cd DoMonit |
| 53 | +pip install -r requirements.txt |
| 54 | +python examples.py |
| 55 | +``` |
| 56 | + |
| 57 | +This is the example script: |
| 58 | + |
| 59 | +``` |
| 60 | +from api.containers import Containers |
| 61 | +from api.ids import Ids |
| 62 | +from api.inspect import Inspect |
| 63 | +from api.logs import Logs |
| 64 | +from api.process import Process |
| 65 | +from api.changes import Changes |
| 66 | +from api.stats import Stats |
| 67 | +
|
| 68 | +
|
| 69 | +import json |
| 70 | +
|
| 71 | +
|
| 72 | +c = Containers() |
| 73 | +i = Ids() |
| 74 | +
|
| 75 | +print ("Number of containers is : %s " % (sum(1 for i in i.ids()))) |
| 76 | +
|
| 77 | +if __name__ == "__main__": |
| 78 | +
|
| 79 | + for c_id in i.ids(): |
| 80 | +
|
| 81 | + ins = Inspect(c_id) |
| 82 | + sta = Stats(c_id) |
| 83 | + proc = Process(c_id, ps_args = "aux") |
| 84 | +
|
| 85 | +
|
| 86 | +
|
| 87 | + # Container name |
| 88 | + print ("\n#Container name") |
| 89 | + print ins.name() |
| 90 | + |
| 91 | + # Container id |
| 92 | + print ("\n#Container id") |
| 93 | + print ins.id() |
| 94 | +
|
| 95 | + # Memory usage |
| 96 | + mem_u = sta.usage() |
| 97 | +
|
| 98 | + # Memory limit |
| 99 | + mem_l = sta.limit() |
| 100 | +
|
| 101 | + # Memory usage % |
| 102 | + print ("\n#Memory usage %") |
| 103 | + print int(mem_u)*100/int(mem_l) |
| 104 | +
|
| 105 | +
|
| 106 | + # The number of times that a process of the cgroup triggered a "major fault" |
| 107 | + print ("\n#The number of times that a process of the cgroup triggered a major fault") |
| 108 | + print sta.pgmajfault() |
| 109 | + |
| 110 | +
|
| 111 | + # Same output as ps aux in *nix |
| 112 | + print("\n#Same output as ps aux in *nix") |
| 113 | + print proc.ps() |
| 114 | +``` |
| 115 | + |
| 116 | +For the following 5 running containers: |
| 117 | + |
| 118 | +``` |
| 119 | +docker ps |
| 120 | +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 121 | +1a29e9652822 instavote/vote "gunicorn app:app -b " 12 seconds ago Up 11 seconds 80/tcp, 100/tcp vote_webapp_3 |
| 122 | +6ca598188d1a instavote/vote "gunicorn app:app -b " 12 seconds ago Up 11 seconds 80/tcp, 100/tcp vote_webapp_4 |
| 123 | +7f1a6bfaf95b instavote/vote "gunicorn app:app -b " 12 seconds ago Up 11 seconds 80/tcp, 100/tcp vote_webapp_5 |
| 124 | +e3a7066ba953 instavote/vote "gunicorn app:app -b " 6 days ago Up 5 hours 80/tcp, 100/tcp vote_webapp_2 |
| 125 | +1e557c8dc5f7 instavote/vote "gunicorn app:app -b " 6 days ago Up 5 hours 80/tcp, 100/tcp vote_webapp_1 |
| 126 | +``` |
| 127 | + |
| 128 | +the result of the above script is: |
| 129 | + |
| 130 | +``` |
| 131 | +Number of containers is : 5 |
| 132 | +
|
| 133 | +#Container name |
| 134 | +/vote_webapp_3 |
| 135 | +
|
| 136 | +#Container id |
| 137 | +1a29e9652822447a440799306f4edb65003bca9cdea4c56e1e0ba349d5112d3e |
| 138 | +
|
| 139 | +#Memory usage % |
| 140 | +0.697797903077 |
| 141 | +
|
| 142 | +#The number of times that a process of the cgroup triggered a major fault |
| 143 | +15 |
| 144 | +
|
| 145 | +#Same output as ps aux in *nix |
| 146 | +{u'Processes': [[u'root', u'26636', u'0.0', u'0.2', u'76808', u'16228', u'?', u'Ss', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26773', u'0.0', u'0.2', u'88776', u'19976', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26784', u'0.0', u'0.2', u'88572', u'19800', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26787', u'0.0', u'0.2', u'88568', u'19816', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26793', u'0.0', u'0.2', u'88572', u'19828', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']} |
| 147 | +
|
| 148 | +#Container name |
| 149 | +/vote_webapp_4 |
| 150 | +
|
| 151 | +#Container id |
| 152 | +6ca598188d1a0762a0c41fa9d3f3df53adb73e3ec71279125595a4f1cd412538 |
| 153 | +
|
| 154 | +#Memory usage % |
| 155 | +0.708284566087 |
| 156 | +
|
| 157 | +#The number of times that a process of the cgroup triggered a major fault |
| 158 | +18 |
| 159 | +
|
| 160 | +#Same output as ps aux in *nix |
| 161 | +{u'Processes': [[u'root', u'26609', u'0.0', u'0.2', u'76808', u'16184', u'?', u'Ss', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26759', u'0.0', u'0.2', u'88776', u'19996', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26760', u'0.0', u'0.2', u'88768', u'20036', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26761', u'0.0', u'0.2', u'88568', u'19836', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26774', u'0.0', u'0.2', u'88572', u'19848', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']} |
| 162 | +
|
| 163 | +#Container name |
| 164 | +/vote_webapp_5 |
| 165 | +
|
| 166 | +#Container id |
| 167 | +7f1a6bfaf95b731440525027964b07572fd2add17b3c245f6658186b71b4010d |
| 168 | +
|
| 169 | +#Memory usage % |
| 170 | +0.699332536688 |
| 171 | +
|
| 172 | +#The number of times that a process of the cgroup triggered a major fault |
| 173 | +5 |
| 174 | +
|
| 175 | +#Same output as ps aux in *nix |
| 176 | +{u'Processes': [[u'root', u'26560', u'0.0', u'0.2', u'76808', u'15960', u'?', u'Ss', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26770', u'0.0', u'0.2', u'88776', u'19904', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26780', u'0.0', u'0.2', u'88572', u'19708', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26785', u'0.0', u'0.2', u'88568', u'19716', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'26786', u'0.0', u'0.2', u'88572', u'19732', u'?', u'S', u'15:43', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']} |
| 177 | +
|
| 178 | +#Container name |
| 179 | +/vote_webapp_2 |
| 180 | +
|
| 181 | +#Container id |
| 182 | +e3a7066ba9534a53bbbf38248688c15c84e2839bac6df637d515f7f5c2383c5b |
| 183 | +
|
| 184 | +#Memory usage % |
| 185 | +0.753300485353 |
| 186 | +
|
| 187 | +#The number of times that a process of the cgroup triggered a major fault |
| 188 | +52 |
| 189 | +
|
| 190 | +#Same output as ps aux in *nix |
| 191 | +{u'Processes': [[u'root', u'3136', u'0.0', u'0.1', u'76768', u'14732', u'?', u'Ss', u'11:54', u'0:04', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3419', u'0.0', u'0.2', u'88544', u'18940', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3420', u'0.0', u'0.2', u'88548', u'18988', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3421', u'0.0', u'0.2', u'88552', u'18996', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3431', u'0.0', u'0.2', u'88548', u'18888', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']} |
| 192 | +
|
| 193 | +#Container name |
| 194 | +/vote_webapp_1 |
| 195 | +
|
| 196 | +#Container id |
| 197 | +1e557c8dc5f7307b51c5638f106b004f68b8d1d2a2f0e53d6de75cac84137656 |
| 198 | +
|
| 199 | +#Memory usage % |
| 200 | +0.738209921509 |
| 201 | +
|
| 202 | +#The number of times that a process of the cgroup triggered a major fault |
| 203 | +61 |
| 204 | +
|
| 205 | +#Same output as ps aux in *nix |
| 206 | +{u'Processes': [[u'root', u'3137', u'0.0', u'0.1', u'76768', u'14532', u'?', u'Ss', u'11:54', u'0:04', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3446', u'0.0', u'0.2', u'88556', u'18724', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3447', u'0.0', u'0.2', u'88552', u'18620', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3449', u'0.0', u'0.2', u'88552', u'18880', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'3463', u'0.0', u'0.2', u'88556', u'18780', u'?', u'S', u'11:54', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']} |
| 207 | +``` |
| 208 | + |
| 209 | +# ToDo |
| 210 | +- Documentation |
| 211 | +- Exception & timeout handling |
| 212 | + |
0 commit comments