Tesla API with Icinga / Nagios

Before my Tesla arrived I have been looking into the API as published on Apiary which was documented by Tim Dorr . Some excellent work and with a few youtube videos it was quite easy to create my own PHP based plugin for Icinga / Nagios.

What can I monitor?

The plugin I wrote (hacked together might be a better expression) enables me to poll the Tesla API on a 5 minute basis. The information returned from the API is in JSON format and easily parsed with PHP.

The API is clear and simple. At first you need to have a token. This token you can get with a simple request to “https://owner-api.teslamotors.com/oauth/token”

        $client_id = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384";

        $client_secret = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";


        $serial_id = "????????";


 // first make sure we get the token


$username = "??????@?????.com";

$password = "???????????";  


        $data = array('grant_type' => 'password', 'client_id' => $client_id, 'client_secret' => $client_secret, 'email' => $username, 'password' => $passwo

rd);

        $header = "Content-Type:application/json\r\n";


        // print_r($data);


        $url ="https://owner-api.teslamotors.com/oauth/token";

        $options = array(

        'http' => array(

                'header'  => $header,

                'method'  => 'POST',

                'content' => http_build_query($data)

                )

        );


        $data_string = json_encode($data);


        $ch = curl_init('https://owner-api.teslamotors.com/oauth/token');

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(

                'Content-Type: application/json',

                'Content-Length: ' . strlen($data_string))

        );


        $result = curl_exec($ch);

        $result = json_decode($result);


        $token = $result->access_token;

                

        echo("token: $token\n");

Because I also want to be able to do some analysis later with my car data I decided to write all returned data first to a Mysql database.  The final Nagios/Icinga plugin is just reading the latest record from the table and puts this information into Icinga.

Example data from Graphite (charging power)

For now I focus on the charging information and battery status. The other data (like driving speed etc.) is nice to have and already stored but not yet in use.

Snippet of API request

Using the SQL data exported to Excel it’s nice to have a bit of insight of the charging speeds, levels voltage and power. 

If you look at a longer period of data (a few days) you can monitor the battery usage and charge speeds.

SaveSave

SaveSave

SaveSave

SaveSave

SaveSave

SaveSave

SaveSave