Skip to content

Commit e49726c

Browse files
committed
initial commit
0 parents  commit e49726c

15 files changed

+2704
-0
lines changed

.gitignore

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
<<<<<<< HEAD
91+
=======
92+
93+
# Specefic
94+
domonit_init.py
95+
>>>>>>> b8b2785a2377c5b0ac0eaf66a435976641a5a002

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Aymen EL Amri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+

__init__.py

Whitespace-only changes.

api/__init__.py

Whitespace-only changes.

api/changes.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import requests_unixsocket
2+
import json
3+
from errors import NoSuchContainerError, ServerErrorError
4+
5+
6+
from lib.utils import Utils
7+
u = Utils()
8+
9+
10+
#https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/
11+
class Changes():
12+
13+
14+
def __init__(self, container_id):
15+
self.container_id = container_id
16+
17+
self.base = "http+unix://%2Fvar%2Frun%2Fdocker.sock"
18+
self.url = "/containers/%s/changes" % (self.container_id)
19+
20+
self.session = requests_unixsocket.Session()
21+
try:
22+
self.resp = self.session.get( self.base + self.url)
23+
except Exception as ex:
24+
template = "An exception of type {0} occured. Arguments:\n{1!r}"
25+
message = template.format(type(ex).__name__, ex.args)
26+
print message
27+
28+
29+
30+
def changes(self):
31+
resp = self.resp
32+
url = self.url
33+
34+
resp_status_code = resp.status_code
35+
u.check_resp(resp_status_code, url)
36+
37+
respj = self.resp.json()
38+
return('{}'.format( respj) )

0 commit comments

Comments
 (0)