Skip to content

Commit a59c581

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#296 from tommy3001/master
Psutil system resources library added to scenarios/admin
2 parents 327099a + 32a14b7 commit a59c581

File tree

3 files changed

+63
-55
lines changed

3 files changed

+63
-55
lines changed

docs/scenarios/admin.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ State files can be written using YAML, the Jinja2 template system or pure python
120120

121121
`Salt Documentation <http://docs.saltstack.org/en/latest/index.html>`_
122122

123+
124+
Psutil
125+
------
126+
`Psutil <https://code.google.com/p/psutil/>`_ is an interface to different system information (e.g. CPU, memory, disks, network, users and processes).
127+
128+
Here is an example to be aware of some server overload. In case of some failed test (net, CPU) it send an email.
129+
130+
.. code-block:: python
131+
132+
# Functions to get system values:
133+
from psutil import cpu_percent, net_io_counters
134+
# Functions to take a break:
135+
from time import sleep
136+
# Package for email services:
137+
import smtplib
138+
import string
139+
MAX_NET_USAGE = 400000
140+
MAX_ATTACKS = 4
141+
attack = 0
142+
counter = 0
143+
while attack <= MAX_ATTACKS:
144+
sleep(4)
145+
counter = counter + 1
146+
# Check the cpu usage
147+
if cpu_percent(interval = 1) > 70:
148+
attack = attack + 1
149+
# Check the net usage
150+
neti1 = net_io_counters()[1]
151+
neto1 = net_io_counters()[0]
152+
sleep(1)
153+
neti2 = net_io_counters()[1]
154+
neto2 = net_io_counters()[0]
155+
# Calculate the bytes per second
156+
net = ((neti2+neto2) - (neti1+neto1))/2
157+
if net > MAX_NET_USAGE:
158+
attack = attack + 1
159+
if counter > 25:
160+
attack = 0
161+
counter = 0
162+
# Write a very important email if attack is higher then 4
163+
TO = "you@your_email.com"
164+
FROM = "webmaster@your_domain.com"
165+
SUBJECT = "Your domain is out of system resources!"
166+
text = "Go and fix your server!"
167+
BODY = string.join(("From: %s" %FROM,"To: %s" %TO,"Subject: %s" %SUBJECT, "",text), "\r\n")
168+
server = smtplib.SMTP('127.0.0.1')
169+
server.sendmail(FROM, [TO], BODY)
170+
server.quit()
171+
172+
173+
A full terminal application like a widely extended top which is based on psutil and with the ability of a client-server
174+
monitoring is `glance <https://github.com/nicolargo/glances/>`_.
175+
176+
123177
Chef
124178
----
125179

docs/scenarios/imaging.rst

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,20 @@ Python Imaging Library
99
----------------------
1010

1111
The `Python Imaging Library <http://www.pythonware.com/products/pil/>`_, or PIL
12-
for short, is *the* library for image manipulation in Python.
12+
for short, is *the* library for image manipulation in Python. Unfortunately, its
13+
development has stagnated, with its last release in 2009.
1314

14-
It works with Python 1.5.2 and above, including 2.5, 2.6 and 2.7. Unfortunately,
15-
it doesn't work with 3.0+ yet.
15+
Lucky for you, there's an actively-developed fork of PIL called `Pillow <http://python-imaging.github.io/>`_ -
16+
it's easier to install, runs on all operating systems, and supports Python 3.
1617

1718
Installation
1819
~~~~~~~~~~~~
1920

20-
PIL has a reputation of not being very straightforward to install. Listed below
21-
are installation notes on various systems.
21+
Before installing Pillow, you'll have to install Pillow's prerequisites. Find
22+
the instructions for your platform `here <https://pypi.python.org/pypi/Pillow/2.1.0#platform-specific-instructions>`_.
2223

23-
Also, there's a fork named `Pillow <http://pypi.python.org/pypi/Pillow>`_ which is easier
24-
to install. It has good setup instructions for all platforms.
25-
26-
Installing on Linux
27-
~~~~~~~~~~~~~~~~~~~
28-
29-
Arch Linux
30-
``````````
31-
32-
PIL is maintained in the official community repository, and installed with the system installer as:
33-
34-
.. code-block:: bash
35-
36-
$ sudo pacman -S python2-imaging
37-
38-
Ubuntu 12.10
39-
````````````
40-
41-
Can be installed on the command line as:
42-
43-
.. code-block:: bash
44-
45-
$ sudo apt-get install python-imaging
46-
47-
48-
Installing on Mac OS X
49-
~~~~~~~~~~~~~~~~~~~~~~
50-
51-
PIP doesn't know about the Mac OS X Freetype paths. To rectify that:
52-
53-
.. code-block:: bash
54-
55-
$ ln -s /usr/X11/include/freetype2 /usr/local/include/
56-
$ ln -s /usr/X11/include/ft2build.h /usr/local/include/
57-
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/
58-
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/libfreetype.dylib
59-
60-
then:
24+
After that, it's straightforward:
6125

6226
.. code-block:: bash
6327
64-
$ brew install libjpeg
65-
$ pip install PIL
66-
67-
68-
Installing on Windows
69-
~~~~~~~~~~~~~~~~~~~~~
70-
71-
.. todo::
72-
Notes on installing on Windows machines
73-
74-
28+
$ pip install Pillow

docs/scenarios/web.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ to use.
319319
import tornado.ioloop
320320
import tornado.web
321321
322-
# Load tamplate file templates/site.html
322+
# Load template file templates/site.html
323323
TEMPLATE_FILE = "site.html"
324324
templateLoader = FileSystemLoader( searchpath="templates/" )
325325
templateEnv = Environment( loader=templateLoader )

0 commit comments

Comments
 (0)