Part 2. psutil - initialstate/pi-health-dashboard GitHub Wiki

We will use psutil to get easy access to the majority of the system information we will use to build our dashboard. To install the psutil Python library, go to a terminal on your Pi and type:

$ sudo pip install psutil

After installation completes, we simply need to run a Python script to begin data collection. Let's put this script in its own directory.

$ cd /home/pi/
$ mkdir pihealth
$ cd pihealth

We are going to copy and modify the Python script at https://github.com/InitialState/pi-health-dashboard/blob/master/pihealth.py. On line 8, you will see the User Settings section:

# --------- User Settings ---------
# Initial State settings
BUCKET_NAME = ":computer: Pi3 Performance" 
BUCKET_KEY = "pi0708"
ACCESS_KEY = "PUT YOUR INITIAL STATE ACCESS_KEY HERE"
# Set the time between checks
MINUTES_BETWEEN_READS = 1
METRIC_UNITS = False
# ---------------------------------

You will need to place your Initial State access key (found in your Initial State account settings) in the ACCESS_KEY variable assignment. If you do not do this, your data will not be streamed into your Initial State account, making you very sad and frustrated.

Note: You should copy/paste your Initial State ACCESS_KEY if at all possible to avoid any mistakes. Your ACCESS_KEY is case sensitive.

The MINUTES_BETWEEN_READS variable is important to set based on your application. If you are going to run this script over the course of days/weeks/months, you will want to make this a larger number (like every 2-5 minutes). If you are running this script to monitor the performance of a short-term application, you may want an update every few seconds (e.g. .1 minutes).

You can use nano, a command-line based text editor, to copy the contents of pihealth.py to your Pi.

$ nano pihealth.py

Copy + Paste the contents of your modified pihealth.py script. CTRL+x to save.

We are ready to start collecting system data.

$ python pihealth.py

Tip: If you want to run this script in the background and be sure that it doesn't exit if your SSH connection is broken or the terminal is closed, you can use the following command:

$ nohup python pihealth.py &

python.h error: If you get an error referencing python.h whenever you try to use psutil in a script, installing the python development libraries may resolve the error:

$ sudo apt-get install gcc python-dev
$ sudo pip install psutil 

<< Part 1: Initial State - Part 3: Customize Your Dashboard >>