InitialState Uptime.

Boy, I wish I knew InitialState before. The thing is great, and it is so easy to use. So, how do I monitor the uptime of my solar rig? Easy peasy.

  • First, you create an account on initalstate.com. Can’t be easier. It will give you an “access key”
  • Second, you create what they call a “data bucket”. A data bucket is a file where you throw data which initialstate will let you visualize. The moment you submit this data to them is recorded, and then you don’t need to bother with it. EDIT: read my next post about this On the bad side, you can’t send events in the past (like my last 10 months of data, as they just show up as from today). I created a bucket called “Uptime”.

Screen Shot 2016-07-13 at 01.12.45.png

  • It gives you place to choose a name, and a Key. These are important.

Screen Shot 2016-07-13 at 01.12.15.png

So, you have your data repository. Now it’s a matter of add data to this bucket. I saved my bucket key and my access key in files called in this example, not surprisingly, ACCESS_KEY and BUCKET_KEY.

Its’s important to notice that both are in the “Endpoint URL” you see above. So, if you don’t know what I am talking about, copy the ENDPOINT URL (or API ENDPOINT), paste it somewhere, and you will have something like this:


This is all you need to know to start submitting data. Shall we?

You can use any language you want to submit data, and I won’t bother with that, because it’s not necessary. All I need is to send two things: my uptime, and the time the Raspberry Pi rebooted. BASH is more than enough for that.

I created two simple files: uptime.sh and reboot.sh. Both are triggered by the Pi’s cron scheduler. I will explain everything, line by line.

The easiest one is the reboot, so I will start with it. It goes like this (copy from github):

REBOOT_DATE=$(date +%Y-%m-%d)
REBOOT_HOUR=$(date +%H:%M)
( sleep 30; curl 'https://groker.initialstate.com/api/events?accessKey='$ACCESS_KEY'&bucketKey='$BUCKET_KEY'&Event=Last%20Reboot:'$REBOOT_DATE'%20'$REBOOT_HOUR ) &

In the first two lines, I assign the contents of the files ACCESS_KEY and BUCKET_KEY to the variables of the same name. So I don’t need to save my scripts with sensitive data.

Next, I have the REBOOT_DATE and REBOOT_HOUR. I am just saving the time when the machine restarted here.

Next, it’s more interesting. It looks like this:

( sleep 30; curl 'https://groker.initialstate.com/api/events?accessKey='$ACCESS_KEY'&bucketKey='$BUCKET_KEY'&Event=Last%20Reboot:'$REBOOT_DATE'%20'$REBOOT_HOUR ) &

First, the command is between parentheses (). Why?

That’s because I send it to the background, with the “&“. But why?

Well, turns out that this script is run when the Raspberry PI is restarting, so it might or might not have a proper internet connection already. So, what I do is to send this whole command to the background – and the command waits 30 seconds, for the network to connect, and then calls:

curl 'https://groker.initialstate.com/api/events?accessKey='$ACCESS_KEY'&bucketKey='$BUCKET_KEY'&Event=Last%20Reboot:'$REBOOT_DATE'%20'$REBOOT_HOUR

In case you didn’t notice, this was inside the parentheses. But what does it do?

Essentially, it sends a request to inisialstate, saying which bucket you want to send to, with the proper keys, and with the time of the latest reboot. It will be shown as something like this:

Screen Shot 2016-07-13 at 01.40.44.png

Notice that I called it “Event” in the URL. It’s just to have one single place to show me anything important going on on my machine.

Now, let’s go to the “uptime” thing, shall we?

This is the script (again, available on github):

UPTIME=$(cat /proc/uptime | cut -f1 -d.)
ACCESS_KEY=$(cat $HOME/.ssh/initialstate-accesskey)
KEY=$(cat $HOME/.ssh/initialstate-uptime-bucket)
echo "insert into solaruptime values(strftime('%Y-%m-%d','now'),strftime('%H:%M','now'),\"$UPTIME\");" | sqlite3 ~/Devel/Solar/uptime.db
curl 'https://groker.initialstate.com/api/events?accessKey='$ACCESS_KEY'&bucketKey='$KEY'&Uptime='$UPTIME

The first line of this script collects the time the machine has been up:

UPTIME=$(cat /proc/uptime | cut -f1 -d.)

the “cut” utility is one of the most useful ones on any UNIX system. Learn it, with “man cut”, and if you don’t get it, write me!

Then, I proceed to get my keys. I already explained that.

Next, I save the uptime data on my personal database, using sqlite. This is the line:

echo "insert into solaruptime values(strftime('%Y-%m-%d','now'),strftime('%H:%M','now'),\"$UPTIME\");" | sqlite3 ~/Solar/uptime.db

With this command, I create a text (using “echo”), which I will send to the command “sqlite3”. This text itself is a standard SQL string. So, besides having my data on initialstate, I have it on my personal database.

The last line is pretty much as the reboot one, except that instead of sending a text, it sends a number, and initialstate will interpret it differently:

curl 'https://groker.initialstate.com/api/events?accessKey='$ACCESS_KEY'&bucketKey='$KEY'&Uptime='$UPTIME

This will just graph my uptime. I don’t have enough data yet, except that I rebooted the machine to check both. You can see the huge uptime, and then a sudden drop, accounting for my forced test reboot:

Screen Shot 2016-07-13 at 01.40.44.png

In order to put those scripts to run, you use the crontab. For that, you type

crontab -e

and add the following lines (mind where you save those files):

@reboot /home/pi/reboot.sh
*/5 * * * * /home/pi/saveuptime.sh

The first line will run after every reboot, waiting for some time until it runs, as you saw above. The second line means that every 5 minutes, the “saveuptime.sh” will run.

Well, that’s pretty much it. It’s much simpler that it seems. I recommend you to try!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s