Skip to content

Instantly share code, notes, and snippets.

@weldpua2008
Created August 15, 2018 08:39
Show Gist options
  • Save weldpua2008/b4e38d3a102a70a94eacae655a04d207 to your computer and use it in GitHub Desktop.
Save weldpua2008/b4e38d3a102a70a94eacae655a04d207 to your computer and use it in GitHub Desktop.
Example LLD of Jenkins Jobs for Zabbix
#!/usr/bin/env python
###################################################################################
# desc: simple tool use to monitoring and discover jenkins jobs
###################################################################################
# Author Valeriy SOloviov <weldpua2008@gmail.com>
# - 21.12.2017
#############################################################
import json
import requests
USERNAME = 'weldpua2008@gmail.com'
PASSWORD = ''
JENKINS_URL = 'https://jenkins:8443/jenkins/'
def discovery(url):
# print url
data = []
try:
jobs = requests.get(url, auth=(USERNAME, PASSWORD))
json_data = json.loads(jobs.text)
if "buildable" in json_data and json_data["buildable"] == True:
if 'fullName' in json_data and 'url' in json_data:
data.append({"{#JOBNAME}": json_data['fullName'],"{#JOBURL}": json_data['url']})
elif "jobs" in json_data:
for job in json_data["jobs"]:
if "url" in job:
data += discovery(job["url"]+'api/json')
except Exception as e:
return []
return data
_data = discovery(JENKINS_URL+'api/json')
data = {"data": _data }
print json.dumps(data)
@weldpua2008
Copy link
Author

Jenkins Job checksum

#!/usr/bin/env bash

JOBURL="$3"
output="error"
cache_expiration=2
# when not updated cache will cause the issue
cache_invalidation=1440
EMPTY_MD5=
[[ "$HOME" = "" ]] && HOME=/var/lib/zabbix


mkdir -p $HOME/.jenkins_cache/ 2> /dev/null

if [[ "${JOBURL}" = "" ]];then
        output="empty joburl"

else
        CUR_TIMESTAMP="$(date '+%s')"
        CACHE_FILE=$HOME/.jenkins_cache/.cache_$(echo "${JOBURL}" | md5sum 2> /dev/null| cut -f1 -d ' ')
        cache_filemtime=$(stat -c '%Y' "${CACHE_FILE}" 2> /dev/null)
        [[ "$cache_filemtime" = "" ]] && cache_filemtime=0
        if [[ "$cache_filemtime" != "0" ]];then
            [[ ! -e "${CACHE_FILE}" ]] && cache_filemtime=0
        fi

        if [[ `expr $CUR_TIMESTAMP - $cache_filemtime` -gt $(( cache_expiration * 60 )) ]];then
            # $((cache_filemtime+60*cache_expiration)) -ge ${CUR_TIMESTAMP} ];then
            code=`curl -sL --user "$1:$2" --connect-timeout 20 --max-time 30 -w "%{http_code}\\n" "$3/config.xml" -o /dev/null`
            if [[ "$code" = "200" ]] || [[ "$code" == 4* ]] ; then
                response=$(curl -s --user "$1:$2" "$3/config.xml" 2> /dev/null)
                if [[ "${response}" != "" ]];then
                        mkdir -p $(dirname "$CACHE_FILE") 2> /dev/null
                        echo "${response}" > ${CACHE_FILE}
                fi
            fi
        fi
        cache_filemtime=$(stat -c '%Y' "${CACHE_FILE}" 2> /dev/null)
        [[ "$cache_filemtime" = "" ]] && cache_filemtime=0
        if [[ "$cache_filemtime" != "0" ]];then
            [[ ! -e "${CACHE_FILE}" ]] && cache_filemtime=0
        fi

        if [[ ! -e "${CACHE_FILE}" ]];then
            echo "${CACHE_FILE} is not exist"
            exit 1
        elif  [[ `expr $CUR_TIMESTAMP - $cache_filemtime` -gt $(( cache_invalidation * 60 )) ]];then
            echo "${CACHE_FILE} is expiried"
            exit 2
        fi
        x=$(cat "${CACHE_FILE}" 2> /dev/null | md5sum 2> /dev/null | cut -f1 -d ' ')
        b=$(echo $((0x${x%% *})))
        output=`echo $b|awk ' { if($1>=0) { print $1} else {print $1*-1 }}'`

fi

echo $output```

@weldpua2008
Copy link
Author

Job aborted

#!/usr/bin/env bash
last_build_url=$(curl -s --user "$1:$2" "$3/api/json"|jq .lastBuild.url|sed -e 's/^"//' -e 's/"$//')
if [[ $(curl -s --user "$1:$2" "$last_build_url/api/json"| jq .result|sed -e 's/^"//' -e 's/"$//') = "ABORTED" ]]; then echo 0; else echo 1; fi

Job Status

if [ $(curl -s --user "$1:$2" "$3/api/json"| jq .color|sed -e 's/^"//' -e 's/"$//') = "disabled" ]; then echo 0; else echo 1; fi
```bash
Job stream
```bash
#!/usr/bin/env bash

code=`curl -sL --user "$1:$2" --connect-timeout 10 --max-time 30 -w "%{http_code}\\n" "$3/api/json" -o /dev/null`
if [[ "$code" = "200" ]]; then
	curl -s --user "$1:$2" "$3/api/json"| jq ."$4"|jq  length
elif [[ "$code" == 4* ]]; then
	if [[ "$4" == upstreamProjects* ]]||  [[ "$4" == downstreamProjects* ]];then
		echo 0
	else
		# curl -s --user "$1:$2" "$3/api/json"| jq ."$4"|jq  length
		echo 0
	fi
else
	echo "Response code $code from API"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment