SENSORTAG TEMPERATURE READINGS IN PYTHON
I wanted to wrap up my previous post TI SensorTag Temperature Readings ) with a little python example, thus this write is going to be short and sweet.
Using the bluepy python library (written by Ian Harvey) I’ve been able to to capture temperature readings, then covert them to Fahrenheit.
To demonstrate I captured a couple temperature samples, a few while sitting on my desk, and a few held up to my air condition vent:
TELEGRAF AND MISSING CPU INTERRUPTS
As I’ve been playing around with Telegraf and Grafana , I’ve noticed CPU interrupts and context switches are not apart of the standard metric gathering.
We know vmstat shows this information, and can be shown it in a easy processable list form:
$ vmstat -s
1016888 K total memory
497920 K used memory
184412 K active memory
173296 K inactive memory
518968 K free memory
70276 K buffer memory
86416 K swap cache
522236 K total swap
88460 K used swap
433776 K free swap
18175 non-nice user cpu ticks
0 nice user cpu ticks
17799 system cpu ticks
172172 idle cpu ticks
7214 IO-wait cpu ticks
0 IRQ cpu ticks
2412 softirq cpu ticks
0 stolen cpu ticks
227755 pages paged in
986944 pages paged out
2353 pages swapped in
121572 pages swapped out
458705 interrupts
1467529 CPU context switches
1461773910 boot time
3456 forks
I decided to hack together a little exec plugin for Telegraf using a Python script, running the script will get you standard out JSON:
PYTHON TRACEROUTE STYLE TOOL
Recently, while talking with a some techies I was asked to explain how traceroute works. I was quick to answer what results I expect back from the command, and how to understand that data, but for the life of me I was having issues recalling how TTL works at the foundation.
Later that week I spent some time reading up on it, and click, it all came back.
To reinforce this I decided to write some code. I decided to use Scapy in order to make crafting network packets a breeze (shown below). You should be able to get Scapy right from PyPi:
PROCESS ELASTICSEARCH JSON ON THE SHELL
Lets throw security out the window for a moment. Say we store user accounts with clear text passwords in Elasticsearch , what is the easiest way to use the results in a shell script? We can begin by creating two accounts, one for admin and one for john :
# curl -XPUT localhost:9200/site/people/1?pretty=True -d '
{"name": "admin", "password": "secret", "admin": "true"}
'
{
"_index" : "site",
"_type" : "people",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
# curl -XPUT localhost:9200/site/people/2?pretty=True -d '
{"name": "john", "password": "password", "admin": "false"}
'
{
"_index" : "site",
"_type" : "people",
"_id" : "1",
"_version" : 2,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"created" : false
}
Using curl this is very easy to query, we just use the id :
TELEGRAF LAPTOP BATTERY PLUGIN
Wanted to expand a little on my previous blog post Custom Telegraf Plugin , and decided to do a simple battery monitor. The end result looks something like this:
I decided to read from the file /sys/class/power_supply/BAT0/capacity on my Ubuntu 14.04 machine, this file merely shows the current battery percent:
# cat /sys/class/power_supply/BAT0/capacity
62
All that is needed is a little Python script for converting this output to JSON, my script outputs like this:
CUSTOM TELEGRAF PLUGIN
I just started looking to InfluxDB and Telegraf for collecting data on a Linux machine, then visualizing it with Grafana . I’ve historically used collectd , statsite , and graphite to accomplish the same sort of task, but wanted to see how some of the new software compares.
I’m running a Ubuntu 14.04 LTS virtual machine, so feel free to follow along.
I managed to install the packages from the InfluxDB ubuntu repositories :
$ cat /etc/apt/sources.list.d/influxdb.list
deb https://repos.influxdata.com/ubuntu trusty stable
After adding the repo, and their GPG key, update and install the packages:
CHECK SSL CERTIFICATE'S EXPIRATION
If you ever want to quickly check the expiration date on your HTTPS server’s SSL certificate all you need is OpenSSL , luckily most of your Linux and OSX workstations will already have it installed.
openssl s_client -showcerts -connect **domain.com**:443 </dev/null 2>/dev/null \
| openssl x509 -noout -dates
You should get back a nice and tidy response with a notBefore and a notAfter date:
notBefore=Mar 13 00:00:00 2015 GMT
notAfter=Mar 12 23:59:59 2018 GMT
POSTGRESQL VACUUM SCRIPT
PostgreSQL does have a built in auto vacuum , but sometimes you just want a small script that can be ran through Jenkins to perform the vacuum for you.
Wanted to share with you guys a small Python script I wrote that will perform a VACUUM VERBOSE ANALYZE on every table within a database.
You will need to get psycopg2 installed from PyPi first:
pip install psycopg2
At which point you should be able to use the below script with the correct environment variables to vacuum your database:
RASPBERRY PI – CLEVERBOT VOICE COMMUNICATION
Using my first generation Raspberry Pi and a few USB / analog devices, i’ve been able to create (a rather slow) cleverbot voice communicator.
The reason for the slow down is initialization and listening on the USB microphone,
but other than that everything works as expected.
#!/usr/bin/env python
import speech_recognition as sr
import pyttsx
import cleverbot
print 'Initializing, please wait...'
# define our cleverbot
cb = cleverbot.Cleverbot()
# speech recognizer setup
r = sr.Recognizer()
# engine for text to speech
engine = pyttsx.init()
#engine.setProperty('rate', 20)
while True:
# obtain audio from the microphone
# this is the bit of code that take a long time...
with sr.Microphone() as source:
print("Talk to cleverbot!")
audio = r.listen(source)
phrase = r.recognize_google(audio)
print ' me: %s' % phrase
resp = cb.ask(phrase)
print ' cleverbot: %s' % resp
engine.say(resp)
engine.runAndWait()
JANKY LEGO STOP MOTION
Well the kids have lost interest in Raspberry Pi Python programming for now, but look who’s still at it! The jankyiest of Lego stop motions.
Here was the code I tossed together to make the gif above:
#!/usr/bin/env python2
import os
import time
import shutil
import datetime
import tempfile
import pygame.camera
import pygame.image
import RPi.GPIO as GPIO
save_dir = '/usr/share/nginx/www'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setup(17, GPIO.IN)
pygame.camera.init()
camera = pygame.camera.Camera('/dev/video0')
def make_picture(filename):
raw_input('Ready for picture? ')
camera.start()
image = camera.get_image()
pygame.image.save(image, filename)
camera.stop()
def make_gif(frames=5):
print 'Making you a gif using %s frames, get ready!' % frames
time.sleep(0.5)
dir = tempfile.mkdtemp()
for i in range(frames):
print 'Taking picture!'
make_picture('%s/%s.jpg' % (dir, i))
time.sleep(3)
print 'Converting images to gif, please wait...'
os.system('convert -delay 20 %s/*.jpg %s/animated.gif' % (dir, dir))
filename = '%s.gif' % datetime.datetime.now().isoformat()
shutil.move('%s/animated.gif' % dir, '%s/%s' % (save_dir, filename))
shutil.rmtree(dir)
print 'Complete!'
while True:
if GPIO.input(17):
make_gif()
GPIO.cleanup()
And a picture of the rig: