tag:blogger.com,1999:blog-38606380223324651562024-03-14T05:55:33.229+01:00Raspberry pythonThis is a place to share my Python scripts for the Raspberry Pi and more.Anonymoushttp://www.blogger.com/profile/14694255756810400553noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-3860638022332465156.post-54996551084454241162012-12-23T06:12:00.000+01:002012-12-23T06:12:39.330+01:00Sending remote commands to our Pi thanks to Pastebin<span style="font-family: Trebuchet MS, sans-serif;">I don't really know what this could be useful for, but I had the idea a couple of days ago of needing to send a command to my Pi system while being at work but have no direct access to it or nor the ip, or maybe the Pi is behind a firewall/NAT so I won't connect to it even with the IP.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">So I thougth, what about a script that checks a page which I can acces from anywhere, downloads a file with the commands, executes them and sends me the result?</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">At first I thougth of doing this with my own server. Pretty easy, you upload to your server a file, let's call it commands.txt, and via cron the RPi checks to see if the file exists, downloads, executes, saves the output to the file and then reuploads it.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">But there is a problem with this. I don't have a server. I had a VPS which expired a couple of weeks ago so I had to improvise.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Enter remote-code.py</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"></span><br />
<a name='more'></a><span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Im gonna paste the <a href="http://pastebin.com/Zz1FFh1S" target="_blank">Pastebin link</a> and then go part by part with the script.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">For this to work you need 2 extra packages. For easy access to Pastebin I used <a href="http://pypi.python.org/pypi/Pastebin/1.0.3" target="_blank">PastebinApi</a> which is in Pypi and to parse the xml I used BeautifulSoup4, which is on Pypi and as a Debian package.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">If you don't have pip installed thats the first thing we are gonna do, plus we will install BeautifulSoup4 at the same time.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> sudo apt-get install python-pip python-bs4
</code></pre>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><span style="font-family: Trebuchet MS, sans-serif;">Now, after the installation we are gonna install pastebin from pip:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> sudo pip install pastebin
</code></pre>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Now we are ready to see the python code.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from pastebin import PastebinAPI
from bs4 import BeautifulSoup
import urllib2, os, logging, datetime
# logging config
logging.basicConfig(filename='remote-order.log',level=logging.DEBUG, format='%(levelname)s-%(asctime)s-%(message)s')
logging.info("Script started")
# variables to configure
developer_key = ""
user_key = ""
user_login = ""
user_password = ""
</code></pre>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><span style="font-family: Trebuchet MS, sans-serif;">As usual with everything on python, the imports go at the start.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;">In this case, Im gonna use the logging lib available from python, to log events to a file.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">We set up the variables at the start. Now the developer key is easy to get in pastebin page but what is the user_key,login and password?</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Well, to list the pastes and access them, we need a user key, and the only way of getting it is to create one using the user/password of the user we want to access. I still have no idea of when the user_key you get expires so there is the posibility of generating a new key each time the program is run, but I believe that would end up with your developer key banned if you keep creating user keys every 5 minutes.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><strike>Anyway, if you need a key just uncomment the couple of lines marked in the code, then run the program once so it prints your key.</strike></span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><span style="font-family: Trebuchet MS, sans-serif;">While I was writing this I just modified the original code. If no user_key is added it will print one and exit so you can copy and paste into the code.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">ok, so the whole program is wrapped in a try,except block in order to catch exceptions and errors and log them. I think its pretty straighforward:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> try:
x = PastebinAPI()
# create the user_key. Not sure if we need to create one every X hours or just one works forever.
if user_key == "":
user_key = x.generate_user_key(developer_key, user_login, user_password)
print "Your user key is: ", user_key
sys.exit(0)
else:
continue
# get the pastes
data = x.pastes_by_user(developer_key, user_key,10)
# parse the data to extract
soup = BeautifulSoup(data)
pastes = soup.find_all("paste_key")
# this is needed to get the first paste we made, as we are using it for the commands
pastes = pastes[len(pastes) - 1]
# download the raw pastebin
url = "http://pastebin.com/raw.php?i=" + pastes.text
cmd = urllib2.urlopen(url)
cmd = cmd.read()
# execute and store the output to a file
os.system(cmd + " > output.txt")
with open("output.txt","r") as f:
output = f.read()
# we paste the output to a new pastebin, private, that expires in 10 minutes
x.paste(developer_key, output, paste_name = "Result of command " + cmd, \
api_user_key = user_key, paste_private = "private", paste_format=None, paste_expire_date = "10M")
# log the finish
logging.info("Program finished ok")
except Exception as e:
logging.critical(e)
except TypeError as e:
logging.critical(e)
finally:
# remove the output.txt file
os.remove("output.txt")
</code></pre>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><span style="font-family: Trebuchet MS, sans-serif;">Ok, as the code is commented, I don't think it needs more explanation that a overview of what it does.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">1 - It get all the pastes by the user </span><br />
<span style="font-family: Trebuchet MS, sans-serif;">2 - Discards all the pastes except the oldest one</span><br />
<span style="font-family: Trebuchet MS, sans-serif;">3 - Downloads the raw paste</span><br />
<span style="font-family: Trebuchet MS, sans-serif;">4 - Executes the command found and redirects the output to a temporal file</span><br />
<span style="font-family: Trebuchet MS, sans-serif;">5 - Creates a new paste with teh title of the command executed, and the output in the body. The paste expires in 10 minutes</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">A couple of things to note. The script checks the oldest paste in the account ALWAYS, so make sure that you edit the oldest paste to make new commands instead of creating new ones. Also, the temporal file we use is useless. The command call should be executed with subprocess.check_output instead but I haven't had time to change it yet. Maybe in version 2 I will?</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">So here is an example:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Here is the pastebin I use for my Raspberry Pi:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDXNnr7xQ8YObsZUmohFtBakHVQfTq2Wxq5OUZZmQejj6sDZe45oRDUJzfv39gJmO_txQPVwQ01TEZwXQbM-QS9ev6LFw91Mn25U1Lb3WbKxWmTPcXpRZpGoy9YjsacFEhA2Zeq7uvobo/s1600/pastes.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDXNnr7xQ8YObsZUmohFtBakHVQfTq2Wxq5OUZZmQejj6sDZe45oRDUJzfv39gJmO_txQPVwQ01TEZwXQbM-QS9ev6LFw91Mn25U1Lb3WbKxWmTPcXpRZpGoy9YjsacFEhA2Zeq7uvobo/s640/pastes.PNG" width="640" /></a></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">As you can see, the first one is called command (name is not important, it's easier to name it something like that)</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Now we enter it, as you can see there is a dot only, that is what I put in there when I don't want any command to be executed, the script just fails. I should improve this to not execute anything and log it.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3nOVhQPMU5RLhXW_pKULRppB5RgwjHECbb6DcTkk94fkcSDOSQIk2WVD2PPk3LHjVpj1DfmvQBWfiyLT68rhxEiZneIQdBiIDvMbIfSs2p8D3lfTvpESANq0ZIjI8Ov6n4-U3GLdAUjk/s1600/pastes1.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3nOVhQPMU5RLhXW_pKULRppB5RgwjHECbb6DcTkk94fkcSDOSQIk2WVD2PPk3LHjVpj1DfmvQBWfiyLT68rhxEiZneIQdBiIDvMbIfSs2p8D3lfTvpESANq0ZIjI8Ov6n4-U3GLdAUjk/s640/pastes1.PNG" width="640" /></a></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Now we just edit it and add the command we want to execute, in this case a ls of the dir /home/pi</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfgswREcfdjq6LSS2JncNSwwrS-Icw1LWYr8o-g9_BeCDTIvyVYdAMrtYvH_4Ajs7MH2rbTrc7UTui95cSvbfpWt0jqStY-H2VH_1vKqd3Q6sywDVviuq-Y_1HLj0bVzNraTXtG4MY5Mc/s1600/pastes2.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfgswREcfdjq6LSS2JncNSwwrS-Icw1LWYr8o-g9_BeCDTIvyVYdAMrtYvH_4Ajs7MH2rbTrc7UTui95cSvbfpWt0jqStY-H2VH_1vKqd3Q6sywDVviuq-Y_1HLj0bVzNraTXtG4MY5Mc/s640/pastes2.PNG" width="640" /></a></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Now I only have to wait until the script is executed by cron, which is every ten minutes.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">To add it to your cron, just execute crontab -e and add the following line (change the path and name of the script if necessary) to have it executed every ten minutes:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> */10 * * * * python /home/pi/scripts/remote-order.py
</code></pre>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><span style="font-family: Trebuchet MS, sans-serif;">Ok, now let's check if we got the new paste...</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieKBMuzsxOJEwQRsjYUtFhYTXwBlx2_OhCmAJgYIiVjIKStFt9v5MpvSvknATuqzgRHaG7SxCk05xPEcpmqscXWqfyG5xbUQctUhKQXM3QsZ5v321ctySarhs8_zy4VGGipXNbNIPPcoU/s1600/pastes3.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="262" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieKBMuzsxOJEwQRsjYUtFhYTXwBlx2_OhCmAJgYIiVjIKStFt9v5MpvSvknATuqzgRHaG7SxCk05xPEcpmqscXWqfyG5xbUQctUhKQXM3QsZ5v321ctySarhs8_zy4VGGipXNbNIPPcoU/s640/pastes3.PNG" width="640" /></a></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /><br /><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /><br /><br /><br /><br /><br /><br />Yep, there it is. A private paste with the correct title that expires in 10 minutes.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Let's see what's inside...</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDPDFAJAGJTr5Ab5y2rAVLW_3TbrGVCV6x9pySO2GTIGH6QK5sQgiYWu8Ez8SKo_sgwEZticENUnECu5Fl9143v3PW-8msdAJ3NR6fW7dmvYzEf3equmpSCcR87EfbeBAMYXJTReILjeE/s1600/pastes4.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="472" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDPDFAJAGJTr5Ab5y2rAVLW_3TbrGVCV6x9pySO2GTIGH6QK5sQgiYWu8Ez8SKo_sgwEZticENUnECu5Fl9143v3PW-8msdAJ3NR6fW7dmvYzEf3equmpSCcR87EfbeBAMYXJTReILjeE/s640/pastes4.PNG" width="640" /></a></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /><br /><br /><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Well, what did we expect? Of course there is the command results in there!</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Okay, now don't forget to go back and edit your "command" paste or else the command will be executed every 10 minutes! Put it back to a dot and call it a day.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">Well, there it is. No idea if it could be useful for anybody or not. But at least it's there. For example, I used it today when I arrived to work and connected the Pi to the network, I had no idea which IP it was on. I have a script that launches every boot that sends an email with the internal and external IP but in this case it didn't launch (the dhcp server at work takes about 3 minutes to kick in and give you an ip) so I used this to re-launch the script and 10 minutes later I got the results. If I didn't have the script I could have send a "ifconfig -a" so I would have get the IP back in a pastebin.</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<span style="font-family: Trebuchet MS, sans-serif;">I hope someone can find a good use for it! Enjoy!</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>Anonymoushttp://www.blogger.com/profile/14694255756810400553noreply@blogger.com0tag:blogger.com,1999:blog-3860638022332465156.post-36711452762883705362012-12-23T04:49:00.001+01:002012-12-23T06:16:46.021+01:00Tracking our Pi System status in Cosm<span style="font-family: Trebuchet MS, sans-serif;"><a href="https://cosm.com/" target="_blank">Cosm.com</a> is a service that allows you to push data to it or it retrieves data from a device/app/cat and will make some incredible nice graphs with it:</span><br />
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwCuHAufuggx9zIMr_lWP1q5Tkv7ijcJ-X0jV1UhjyRT4kcucYBVX0MeEbZ5IXftIQ_UrC-hLHM_hnZWYXK3nzXMj1wkD8fx047qNVY1nRQcDd8J7hTIrMGKYwO5ROrOLH2J3pNybzLFI/s1600/cosm.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwCuHAufuggx9zIMr_lWP1q5Tkv7ijcJ-X0jV1UhjyRT4kcucYBVX0MeEbZ5IXftIQ_UrC-hLHM_hnZWYXK3nzXMj1wkD8fx047qNVY1nRQcDd8J7hTIrMGKYwO5ROrOLH2J3pNybzLFI/s400/cosm.PNG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">They have a nice API and they accept XML, JSON and CSV to update out data.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">So it's a nice platform to control and build graphs for our Raspberry Pi system. With it we can control and track how our system behaves along time without accesing the device.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"></span></div>
<a name='more'></a><span style="font-family: Trebuchet MS, sans-serif;"><br /></span>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">And with a few lines of python, we have a nice script which will upload cpu(in percentage), free hdd space (rootfs), free and used memory. You can see a couple of examples in the image attached.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">Here is the code:</span></div>
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> # cosm.py Copyrigth 2012 Itxaka Serrano Garcia <itxakaserrano@gmail.com>
# licensed under the GPL2
# see the full license at http://www.gnu.org/licenses/gpl-2.0.txt
#
# You only need to add 2 things, YOUR_API KEY HERE and YOUR_FEED_NUMBER_HERE
# also, you can change your stream ids, in that case change the id names in the "data = json.dumps..." line
import json, subprocess, os
hdd = subprocess.check_output(["df | grep rootfs | awk '{print $2,$4,$5}'"], shell=True)
hdd = hdd.split()
hdd = int(hdd[1]) / 1024
cpu = subprocess.check_output(["vmstat | awk '{print $13}'"], shell=True)
cpu = cpu.split()[1]
mem = subprocess.check_output(["cat /proc/meminfo | grep Mem | awk '{print $2}'"], shell=True)
mem = mem.split()
mem_total = int(mem[0]) / 1024
mem_free = int(mem[1]) / 1024
mem_used = mem_total - mem_free
data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"hdd","current_value":hdd },{"id":"cpu","current_value":cpu},{"id":"free_mem","current_value":mem_free},{"id":"used_mem","current_value":mem_used}]})
with open("temp.tmp", "w") as f:
f.write(data)
subprocess.call(['curl --request PUT --data-binary @temp.tmp --header "X-ApiKey: YOUR_IP_KEY_HERE" http://api.cosm.com/v2/feeds/YOUR_FEED_NUMBER_HERE'], shell=True)
os.remove("temp.tmp")
</code></pre>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div class="separator" style="clear: both;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><a href="http://pastebin.com/mhDWtjN7" target="_blank">Here on Pastebin</a></span></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">We can get this working in 4 steps.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">1 - Copy the code and save it to a file in your Pi under whatever name you want (We will use cosm.py in this example) and store it whenever you want, I usually put them in /home/pi/scripts/</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">2 - Add in the code your API key for cosm and your Feed name (Instructions on how to get an api key are in the cosm.com page)</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">3 - Add a new entry in your crontab to run it every X minutes. In my case I run it every 10 minutes so my cron line is</span></div>
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> */10 * * * * python /home/pi/scripts/cosm.py
</code></pre>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">4 - Visit your cosm.com feed page and wait for the results</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">There! That was easy wasn't it?</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;">Be sure to check <a href="https://cosm.com/feeds/94310" target="_blank">my feed</a></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span></div>
<span style="font-family: Trebuchet MS, sans-serif;"><br /></span>Anonymoushttp://www.blogger.com/profile/14694255756810400553noreply@blogger.com8