Jenkins uptime - how long since last restart

旧城冷巷雨未停 提交于 2021-02-06 11:25:20

问题


Is it possible to see how long a Jenkins instance/master has been running?

I've tried looking around in "Manage Jenkins" but can't find it there. I know I could log in and check the process on the machine, but is it possible to do it in Jenkins web UI?

This URL shows a white line when it was restarted... but it's not that intuitive.

<jenkins-url>/monitoring?part=graph&graph=usedMemory&period=mois

回答1:


You can run groovy script on Jenkins web-ui from: Manage Jenkins > Script Console, and use Jenkins API. If you want to know how many days Jenkins has been running:

import java.util.concurrent.TimeUnit
long lastRestarted = Jenkins.instance.toComputer().getConnectTime()
long now =  System.currentTimeMillis()
println TimeUnit.MILLISECONDS.toDays(now - lastRestarted)

getConnectTime() of the master computer should be the time when it restarted. http://javadoc.jenkins.io/hudson/model/Computer.html#getConnectTime()




回答2:


There has been an Uptime class in Jenkins Core since a long time (Jenkins 1.538). So using the same principle with the script console, the code can be made more readable and robust:

println "Jenkins has been started " + (ExtensionList.lookupSingleton(Uptime.class).uptime / 1000 / 60 ) + " minutes ago"

Which will show, e.g.:

Jenkins has been started 175.8678166667 minutes ago

Note: ExtensionList.lookupSingleton was introduced in Jenkins 2.87. So if you're using an earlier version, use ExtensionList.lookup(Uptime.class).get(0) instead.




回答3:


Just check the "all system logs" at <jenkins_url>/log/all - scroll the log to the top.

No need for Groovy scripts.

P.S. But, as pointed out, this option will work only if the restart was recent and the corresponding log entries were not overwritten.



来源:https://stackoverflow.com/questions/40175687/jenkins-uptime-how-long-since-last-restart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!