Tuesday, 1 October 2013

Buildbot monitoring with check_mk

The following script is used on my buildmaster and report back the status and queue lengths for each individual builder.
Currently uses a script to process out the json response from the server so that it can be processed.


#!/bin/bash
url="http://localhost:8010/json"
json=`curl $url 2>/dev/null`
builder=`echo $json |json.sh |grep state |grep "^\[\"builder" |\grep "state\"\]" |awk -F"\"" '{print $4"="$8}'`
set -- `echo $builder`
for i in $*
do
        export `echo $i |cut -d= -f1`=`echo $i |cut -d= -f2`
done

builder_list=`echo $json |json.sh|grep state|grep "^\[\"builder" |\grep "state\"\]" |awk -F"\"" '{print $4}'`
for q in `echo $builder_list`
do
        builder_curl=`curl ${url}/builders/${q} 2>/dev/null`
        state=`echo $builder_curl|json.sh 2>&1 |grep "\"failed\",\"" |grep -v "\[\]" |grep -v "\[\]" |awk '{print $2}' |sed 's/\"//g' |sed 's/\[//g'|sed 's/\]//g'|sed 's/,/:/g'`
        pending=`curl ${url}/builders/${q} 2>/dev/null |json.sh 2>&1 |grep pendingBuilds |sed '/\[\]/ d'|awk '{print $2}'`
        current_step=`curl ${url}/builders/${q}/builds/-1 2>/dev/null|json.sh -l 2>&1|grep  "\"currentStep\",\"text\",0" |awk '{print $2}' |tr -d [\'\"]`
        export $q="${!q},$pending,$state,$current_step"

done

for q in `echo $builder_list`
do
        state=`echo ${!q} |grep failed |wc -l`
        status=`echo ${!q} |cut -d, -f1`
        step=""
        queue="0"
        current="0"
        if [ $state -ne 0 ]
        then
                STATE="WARNING"
                STATUS="1"
                MSG=`echo ${!q} Failed|awk -F"failed:" '{print $2}'`
        else
                STATE="Idle"
                STATUS="0"
                MSG=""
        fi
        if [ $status == "offline" ]
        then
                STATE="Slave Offline"
                STATUS="3"
                MSG=""
        elif [ $status == "building" ]
        then
                build_step=`echo ${!q} |cut -d, -f4`
                STATE="Building"
                current="1"
                queue=`echo ${!q} |cut -d, -f2`
                MSG="$MSG builder queue has $queue jobs waiting. Current build step $build_step"
        fi
        echo -e "$STATUS Pulse_$q ${q}=$current|${q}_queue=$queue $STATE $MSG"
done