<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Prune&#039;s Blog &#187; Python</title>
	<atom:link href="http://www.lecentre.net/blog/archives/category/dev/python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lecentre.net/blog</link>
	<description>Internet, UNIX, Video, Leisure...</description>
	<lastBuildDate>Thu, 19 Jan 2012 20:38:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sending SMS from Nagios</title>
		<link>http://www.lecentre.net/blog/archives/985</link>
		<comments>http://www.lecentre.net/blog/archives/985#comments</comments>
		<pubDate>Thu, 17 Dec 2009 13:10:24 +0000</pubDate>
		<dc:creator>Prune</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://www.lecentre.net/blog/?p=985</guid>
		<description><![CDATA[Sometimes you need to be able to send SMS from a computer. This is obvious when you&#8217;re talking of monitoring your servers. Email alerts are good, but what if you&#8217;re sleeping ? I used to set up a SMS modem, with it&#8217;s own SIM card and subscription. This is really easy, using smsd daemon, to [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-986" title="logo" src="http://www.lecentre.net/blog/wp-content/uploads/2009/12/logo-250x63.png" alt="logo" width="250" height="63" /></p>
<p>Sometimes you need to be able to send SMS from a computer. This is obvious when you&#8217;re talking of monitoring your servers. Email alerts are good, but what if you&#8217;re sleeping ?<br />
I used to set up a SMS modem, with it&#8217;s own SIM card and subscription. This is really easy, using smsd daemon, to send SMS. Just put a formated file in a directory and bam!, you SMS is fired.<br />
But what when you can&#8217;t set a SMS Modem in a datacenter ?</p>
<p>Then you have to use an online SMS provider.</p>
<li>Good</li>
<p><span style="color: #339966;"><span style="background-color: #ffffff;">you just have to call an API to send a SMS</span></span></p>
<li>BAD</li>
<p><span style="color: #ff0000;">it&#8217;s not free (you pay per SMS when the modem can have an unlimited SMS sending subscription<br />
it is network dependant ; if you have a network failure, you will never be informed</span></p>
<p><span id="more-985"></span></p>
<p>Once you know that, you can start using it.<br />
I will set up two monitoring plateforms : one inside the datacenter, monitoring every services and local network and one outside, on a hosted server somewhere else, to check the network connecivity of the datacenter.</p>
<p>As our main monitoring is using Nagios, I had to write scripts for it. I now have 2 python scripts : one for sending SMS, one to check how many SMS I have on my account.</p>
<p>I first had a quick look on the french SMS providers. I chosed <a href="http://www.smsenvoi.com">www.smsenvoi.com</a> as they are one of the cheapest and the http and mail API are free (yes, some providers make you pay for that !!). They are also based in Marseilles. I&#8217;m happy thinking people there can do better than people from Paris <img src='http://www.lecentre.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So, let&#8217;s dig the http API. You can find it at <a title="http://www.smsenvoi.com/pgs_api.php" href="http://www.smsenvoi.com/pgs_api.php" target="_blank">http://www.smsenvoi.com/pgs_api.php</a><br />
It&#8217;s quite simple, even if ugly : you send data in POST request, like : userLogin=your.account@domain.com&amp;userPassword=your_password_clear&amp;&#8230;<br />
And call a page, depending on your need :</p>
<ul>
<li>smsEnvoi.php : send a message</li>
</ul>
<ul>
<li>smsNumber.php : see how many SMS you have availlable in your account</li>
</ul>
<ul>
<li>smsStatus.php and smsMNC.php : check if a SMS have been sent (this is used when you do mass sending)</li>
</ul>
<p>You can do pretty neat things. Just check on their website what the URL&#8217;s are.<br />
To be honest, I only re-write, in a simple way, what the PHP code do but in Python.</p>
<p>You can find the two scripts attached.<br />
<strong>sendSMS.py</strong> take a message from the line input and send it. You have to start it for each recipient. Even if it&#8217;s easy to add many recipient at the same time, Nagios can&#8217;t do that. I&#8217;m just keeping it simple.<br />
<strong>check_sms_free</strong> is a script (nagios check formated) to check how many SMS you still have in your account. The Nagios check is defines to be at WARNING state when only 50 SMS are availlable and in CRITICAL state when only 10 are.</p>
<p>Here are the Nagios config (I do have it in Nconf, but here is what is generated in the statis Nagios file) :</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">
<ol>
<li class="li1">
<div class="de1">define <span class="kw3">command</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">command_name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;host-notify-by-sms</div>
</li>
<li class="li1">
<div class="de1">command_line &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span><span class="kw3">printf</span> <span class="st0">&quot;%b&quot;</span> <span class="st0">&quot;Type: <span class="es2">$NOTIFICATIONTYPE</span>$ &#8211; Host: <span class="es2">$HOSTALIAS</span>$ &#8211; Address: <span class="es2">$HOSTADDRESS</span>$ &#8211; State: <span class="es2">$HOSTSTATE</span>$ &#8211; Date: <span class="es2">$LONGDATETIME</span>$ &#8211; Info: <span class="es2">$HOSTOUTPUT</span>$&quot;</span> <span class="sy0">|</span> <span class="sy0">/</span>opt<span class="sy0">/</span>monitor<span class="sy0">/</span>nagios<span class="sy0">/</span>scripts<span class="sy0">/</span>sendSMS.py <span class="re5">&#8211;login</span>=<span class="re1">$USER8</span>$ <span class="re5">&#8211;passwd</span>=<span class="re1">$USER9</span>$ <span class="re5">&#8211;smsserver</span>=www.smsenvoi.com <span class="re5">&#8211;receivers</span>=+<span class="re1">$CONTACTPAGER</span>$ <span class="re5">&#8211;smsfrom</span>=monitor-prod</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">define <span class="kw3">command</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">command_name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notify-by-sms</div>
</li>
<li class="li1">
<div class="de1">command_line &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span><span class="kw3">printf</span> <span class="st0">&quot;%b&quot;</span> <span class="st0">&quot;Type: <span class="es2">$NOTIFICATIONTYPE</span>$ &#8211; Service: <span class="es2">$SERVICEDESC</span>$ &#8211; Host: <span class="es2">$HOSTALIAS</span>$ &#8211; State: <span class="es2">$SERVICESTATE</span>$ &#8211; Date: <span class="es2">$LONGDATETIME</span>$ &#8211; Info: <span class="es2">$SERVICEOUTPUT</span>$&quot;</span> <span class="sy0">|</span> <span class="sy0">/</span>opt<span class="sy0">/</span>monitor<span class="sy0">/</span>nagios<span class="sy0">/</span>scripts<span class="sy0">/</span>sendSMS.py <span class="re5">&#8211;login</span>=<span class="re1">$USER8</span>$ <span class="re5">&#8211;passwd</span>=<span class="re1">$USER9</span>$ <span class="re5">&#8211;smsserver</span>=www.smsenvoi.com <span class="re5">&#8211;receivers</span>=<span class="re1">$CONTACTPAGER</span>$ <span class="re5">&#8211;smsfrom</span>=monitor-prod</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">define <span class="kw3">command</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">command_name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;check_sms_free</div>
</li>
<li class="li1">
<div class="de1">command_line &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$USER1</span>$<span class="sy0">/</span>check_sms_free <span class="re5">&#8211;login</span>=<span class="re1">$USER8</span>$ <span class="re5">&#8211;passwd</span>=<span class="re1">$USER9</span>$ <span class="re5">&#8211;smsserver</span>=<span class="re1">$ARG1</span>$ <span class="re5">-w</span> <span class="re1">$ARG2</span>$ <span class="re5">-c</span> <span class="re1">$ARG3</span>$</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</div>
<p>Define the username and password for the <strong>smsenvoi</strong> account in resource.cfg as $USER8$ and $USER9$</p>
<p>Then, you can add the check to a host :</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">
<ol>
<li class="li1">
<div class="de1">define service <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">service_description &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check_sms_free</div>
</li>
<li class="li1">
<div class="de1">check_command &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check_sms_free<span class="sy0">!</span>www.smsenvoi.com<span class="sy0">!</span><span class="nu0">50</span><span class="sy0">!</span><span class="nu0">10</span></div>
</li>
<li class="li1">
<div class="de1">host_name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localhost</div>
</li>
<li class="li1">
<div class="de1">check_period &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;24&#215;7</div>
</li>
<li class="li1">
<div class="de1">notification_period &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24&#215;7</div>
</li>
<li class="li1">
<div class="de1">contact_groups &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;admins,sysadmins</div>
</li>
<li class="li1">
<div class="de1">event_handler_enabled &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">active_checks_enabled &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">passive_checks_enabled &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">notifications_enabled &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">check_freshness &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">freshness_threshold &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">86400</span></div>
</li>
<li class="li1">
<div class="de1">use &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Default_timeperiod_interval_5</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</div>
<p>Don&#8217;t forget to define the cellphone number of each users in the &laquo;&nbsp;PAGER&nbsp;&raquo; field :</p>
<div class="codesnip-container" >
<div class="bash codesnip" style="font-family:monospace;">
<ol>
<li class="li1">
<div class="de1">define contact <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">contact_name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sy0">@</span>lecentre.net-sms</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">alias</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sms lecentre</div>
</li>
<li class="li1">
<div class="de1">host_notification_options &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d,r</div>
</li>
<li class="li1">
<div class="de1">service_notification_options &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c,r</div>
</li>
<li class="li1">
<div class="de1">email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">@</span>lecentre.net</div>
</li>
<li class="li1">
<div class="de1">pager &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +<span class="nu0">33682887766</span></div>
</li>
<li class="li1">
<div class="de1">host_notification_period &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;24&#215;7</div>
</li>
<li class="li1">
<div class="de1">service_notification_period &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24&#215;7</div>
</li>
<li class="li1">
<div class="de1">host_notification_commands &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;host-notify-by-sms</div>
</li>
<li class="li1">
<div class="de1">service_notification_commands &nbsp; &nbsp; &nbsp; &nbsp; notify-by-sms</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</div>
<p>Note the format of the phone number, with international format, starting with +<br />
And voila&#8230;<br />
Find the two scripts here : <a title="sendSMS.py" href="http://www.lecentre.net/blog/download/sendSMS.py" target="_blank">sendSMS.py</a> and <a title="check_sms_free" href="http://www.lecentre.net/blog/download/check_sms_free" target="_blank">check_sms_free</a></p>
<p>Todos :<br />
- limit the message size to 160 caracters, which is the max for a single SMS. If you send messages longer, they will count as two SMS.<br />
- catch every exception in the httplib Python framework (this is really basic for the moment)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lecentre.net/blog/archives/985/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Building MacPorts Py-Mysql on Leopard</title>
		<link>http://www.lecentre.net/blog/archives/235</link>
		<comments>http://www.lecentre.net/blog/archives/235#comments</comments>
		<pubDate>Fri, 15 Feb 2008 21:46:24 +0000</pubDate>
		<dc:creator>Prune</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.lecentre.net/blog/archives/235</guid>
		<description><![CDATA[If you installed over your old Tiger or if you are new user, you may have experienced errors when trying to build py-mysql.Py-mysql is a Python module to connect to Mysql.I&#8217;m using it to have Django, the Python Web Framework, to connect to Mysql database. This is what I got when trying to install :   # [...]]]></description>
			<content:encoded><![CDATA[<p>If you installed over your old Tiger or if you are new user, you may have experienced errors when trying to build py-mysql.Py-mysql is a Python module to connect to Mysql.I&#8217;m using it to have Django, the Python Web Framework, to connect to Mysql database. This is what I got when trying to install :  
<pre lang="bash"># port install py-mysql
--->  Building py-mysql with target build
Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_python_py-mysql/work/MySQL-python-1.2.2" &#038;&#038; /opt/local/bin/python2.4 setup.py build " returned error 1
Command output: running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.3-ppc-2.4/MySQLdb
running build_ext
building '_mysql' extension
/usr/bin/gcc-4.0 -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -fno-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/opt/local/include/mysql5/mysql -I/opt/local/Library/Frameworks/Python.framework/Versions/2.4/include/python2.4 -c _mysql.c -o build/temp.macosx-10.3-ppc-2.4/_mysql.o
In file included from /opt/local/include/mysql5/mysql/mysql.h:47,
                 from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate 'unsigned'
/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers
error: command '/usr/bin/gcc-4.0' failed with exit status 1

Error: Status 1 encountered during processing.
</pre>
<p>I tried to upgrade MacPorts&#8230; but I already had the latest.</p>
<pre lang="bash"># port selfupdate

MacPorts base version 1.600 installed

Downloaded MacPorts base version 1.600

The MacPorts installation is not outdated and so was not updated
selfupdate done!</pre>
<p>I finaly found the solution on <a href="http://rob.cogit8.org/blog/2007/Nov/14/installing-django-leopard-mac-os-105/" target="_blank">http://rob.cogit8.org/blog/2007/Nov/14/installing-django-leopard-mac-os-105/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lecentre.net/blog/archives/235/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faire du python dans automator (Leopard)</title>
		<link>http://www.lecentre.net/blog/archives/165</link>
		<comments>http://www.lecentre.net/blog/archives/165#comments</comments>
		<pubDate>Fri, 28 Dec 2007 16:03:29 +0000</pubDate>
		<dc:creator>Prune</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.lecentre.net/blog/archives/165</guid>
		<description><![CDATA[Voila le site d&#8217;une personne qui a une solution. Cela interessera particulierement mon ami Akhen, qui fete son anniversaire demain. Tiens, c&#8217;est pour toi : http://toxicsoftware.com/run-python-script/]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.lecentre.net/blog/wp-content/uploads/2007/12/2110974754_6e5540298b_o.png" alt="http://toxicsoftware.com/run-python-script" /></p>
<p>Voila le site d&#8217;une personne qui a une solution. Cela interessera particulierement mon ami Akhen, qui fete son anniversaire demain. Tiens, c&#8217;est pour toi : <a href="http://toxicsoftware.com/run-python-script/" title="http://toxicsoftware.com/run-python-script/" target="_blank">http://toxicsoftware.com/run-python-script/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lecentre.net/blog/archives/165/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

