Raising the bar with BitBar (Mac)
We now offer a new alert integration for Mac users. Get the script to learn how to implement this integration right away.
Mat Ryer (@matryer) has developed a neat tool for Mac OS X that allows you to put the output from any script or program right into your menu bar. Your script can be written using BASH, Ruby, Python, PHP etc.
After stumbling across this tool, it soon became apparent this would be an excellent next victim candidate to integrate with the Catchpoint Pull API. Below is a screenshot of the tool in action.
To begin, you need to visit https://getbitbar.com and download and install BitBar (it is completely free, but donations are appreciated).
Once you have copied to your Applications folder and created your plugins folder, we can start on the integration. For this, I will mainly be using PHP.
Create your plugin filename by following the convention of {name}.{refresh}.{ext} e.g. catchpoint.5m.php. This will have the plugin run your script every five minutes in BitBar.
Below is an example script that fetches your alerts and formats them nicely for you.
#!/usr/bin/php
<?php
/* Display Catchpoint logo on menu bar */
_echo “| templateImage=iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAA
BWzo5XAAABrklEQVQ4jYXTy2oUURAG4G96ro7xmhARERHcZm3AhUJAxIW4Fl/B1
3Onj6EoatCgIJF4jZO59IyL+TvTMyFY0Jw6df3rr9OcLjdzPsDOiu2ENFfuLWxi
gkc4hz/oYR3beIMNDDA7rVAfj7GGV/EfYIQj/EijO3gb+4lCO7iI90F0kHNUO38
l9jBot/ABipWxGkn6irMYx1fG18IQe9G7dURd3E/wx5zddC0xTfEynBS1SUpcx3
5RC94PR238TdIs3Ayjlxmxgd/mhN9A0cLlBA/yVd1m+F4jtBMOqwadNNjFrMCTF
DhT46hCcryV6MPEjHPvZdRnjQTdDfttiw0N8NOyXEjDdpBPcQsvm7idgNJ8vRVv
zRScpkjbfOWNjCWj9tFvBm5hvs5RrVAj0KvNnrd4AlXeeu5fCnyLYy3GifkbKpL
Yzyf+VvQr4XIPn6sH2Qy5G7hkvv5+DVEz3BQW76gTRJvSsZI+HuJ1RqvezjQoxh
lxgmtB/SKxS/9aK2SOgqqRcY9ih6uxj2LbTaOlQpPMe4h7+JTEdgr34tvGc7yz
2Oh/5Wk42Iq+Ssex/AOGoHohWJhtKAAAAABJRU5ErkJggg==\n”;_
echo “—\n”;
/* Configure Catchpoint API Key/Secret */
$key = ‘<Your_Catchpoint_API_Key’;
$secret = ‘<Your_Catchpoint_API_Secret>’;
/* Get Auth Token */
$data = array(‘grant_type’ => ‘client_credentials’, ‘client_id’ => $key, ‘client_secret’ => $secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://io.catchpoint.com/ui/api/token’);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
/* Base64 encode the token */
$_SESSION[‘token’] = base64_encode($result->access_token);
$ch = curl_init();
/* Fetch last 5 alerts */
curl_setopt($ch, CURLOPT_URL, ‘https://io.catchpoint.com/ui/api/v1/alerts?pageSize=5’);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(‘Authorization: Bearer ‘ . $_SESSION[‘token’]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$alerts = json_decode($data);
/* Apply styling to each alert */
foreach ($alerts->alerts as $a) {
switch ($a->level->id) {
case 0:
$color = ‘#ff8000’;
$state = ‘Warning’;
$emoji = ‘:neutral_face:’;
break;
case 1:
$color = ‘#ff0000’;
$state = ‘Critical’;
$emoji = ‘:worried:’;
break;
case 3:
$color = ‘#80ff00’;
$state = ‘OK’;
$emoji = ‘:smile:’;
break;
}
echo “:clock1030: ” . $a->report_time . ” | size=10 color=” . $color. “\n”;
echo $emoji . ” Test: ” . $a->test->name . ” – ” . $state . “| size= 10 color=” . $color . ” href=http://portal.catchpoint.com/ui/Content/Charts/Performance.aspx?tList=” . $a->test->id . “&z=&chartView=1\n”;
echo “Reason: ” . $a->alert_type->name . ” | size=10 color=#000000\n”;
echo “—\n”;
}
Save this and make sure it is executable (chmod +x catchpoint.5m.php); you will then get your alerts (if you have any) listed in the new plugin e.g.
For additional integration you can add an extra line that will display the last alert received in your Notifications. Add the following line to the bottom of the above script:
echo exec(“osascript -e ‘display notification \”” . $a->report_time . ” – ” . $a->alert_type->name . “\” with title \”” . $a->test->name . ” – ” . $state . “\”‘”);
There you have it … Catchpoint alerts integrated right into your Mac OS X desktop! Enjoy!