jenkins脚本实现监控slave是否掉线

jenkisn提供了api获取node节点状态

http://localhost/computer/slave_name/api/json?pretty=true

只需要将slave_name换成指定的slave名称即可,返回数据是json格式的

{
  "actions" : [
    {
      
    },
    {
      
    }
  ],
  "displayName" : "Niubility_MacPro",
  "executors" : [
    {
      
    },
    {
      
    },
    {
      
    }
  ],
  "icon" : "computer-x.png",
  "iconClassName" : "icon-computer-x",
  "idle" : true,
  "jnlpAgent" : true,
  "launchSupported" : false,
  "loadStatistics" : {
    
  },
  "manualLaunchAllowed" : true,
  "monitorData" : {
    "hudson.node_monitors.SwapSpaceMonitor" : null,
    "hudson.node_monitors.ArchitectureMonitor" : null,
    "hudson.node_monitors.ResponseTimeMonitor" : {
      "timestamp" : 1473177958848,
      "average" : 3112
    },
    "hudson.node_monitors.TemporarySpaceMonitor" : null,
    "hudson.plugins.network_monitor.NameResolutionMonitor" : null,
    "hudson.plugins.systemloadaverage_monitor.SystemLoadAverageMonitor" : null,
    "hudson.node_monitors.DiskSpaceMonitor" : null,
    "hudson.node_monitors.ClockMonitor" : null
  },
  "numExecutors" : 3,
  "offline" : true,
  "offlineCause" : {
    
  },
  "offlineCauseReason" : "Connection was broken: java.io.EOFException\n\tat org.jenkinsci.remoting.nio.NioChannelHub$3.run(NioChannelHub.java:614)\n\tat java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:262)\n\tat hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)\n\tat jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)\n\tat java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:262)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)\n\tat java.lang.Thread.run(Thread.java:745)\n",
  "oneOffExecutors" : [
    
  ],
  "temporarilyOffline" : false
}

如果key offline的value为true说该slave掉线了,offlineCauseReason表面原因。可以写个简单地python脚本,把脚本放到jenkins上定时执行进行监控,最好是限定这个任务在master上执行,保证任务顺利执行。shell里可以判断返回不为空时设置为exit 1,使任务失败,对于任务失败配置邮件(构建后任务里)提醒即可。

import urllib2
import json
baseapi = http://localhost/computer/%s/api/json?pretty=true
def request(slaves):
    slaveapis = slaves.split(",")
    offlineapi = ""
    for api in slaveapis:
        url = baseapi % api
        ret = json.load(urllib2.urlopen(url))
        if ret['offline']:
            offlineapi += url
            offlineapi += ","
    return offlineapi
            
    原文作者:白天才痴
    原文地址: https://www.jianshu.com/p/c9bc35b26c26
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞