无限 运行 Jython Weblogic 脚本

Infinitely run Jython Weblogic Script

以下脚本摘自

https://github.com/RittmanMead/obi-metrics-agent/blob/master/obi-metrics-agent.py

该脚本是用 jython 编写的,它会访问 weblogic 管理控制台以提取指标 问题是它只运行一次,不会无限循环

这是我出于我的目的从原始文件中提取的脚本:

import calendar, time
import sys
import getopt

print '---------------------------------------'

# Check the arguments to this script are as expected.
# argv[0] is script name.
argLen = len(sys.argv)
if argLen -1 < 2:
    print "ERROR: got ", argLen -1, " args, must be at least two."
    print '$FMW_HOME/oracle_common/common/bin/wlst.sh obi-metrics-agent.py  <AdminUserName> <AdminPassword> [<AdminServer_t3_url>] [<Carbon|InfluxDB>] [<target host>] [<target port>] [targetDB influx db>'
    exit()

outputFormat='CSV'
url='t3://localhost:7001'
targetHost='localhost'
targetDB='obi'
targetPort='8086'

try:
    wls_user = sys.argv[1]
    wls_pw = sys.argv[2]
    url  = sys.argv[3]
    outputFormat=sys.argv[4]
    targetHost=sys.argv[5]
    targetPort=sys.argv[6]
    targetDB=sys.argv[7]
except:
    print ''

print wls_user, wls_pw,url, outputFormat,targetHost,targetPort,targetDB

now_epoch = calendar.timegm(time.gmtime())*1000
if outputFormat=='InfluxDB':
    import httplib
    influx_msgs=''

connect(wls_user,wls_pw,url)
results = displayMetricTables('Oracle_BI*','dms_cProcessInfo')

while True:
    for table in results:
        tableName = table.get('Table')
        rows = table.get('Rows')
        rowCollection = rows.values()
        iter = rowCollection.iterator()
        while iter.hasNext():
            row = iter.next()
            rowType = row.getCompositeType()
            keys = rowType.keySet()
            keyIter = keys.iterator()
            inst_name= row.get('Name').replace(' ','-')
            try:
                server= row.get('Servername').replace(' ','-').replace('/','_')
            except:
                try:
                    server= row.get('ServerName').replace(' ','-').replace('/','_')
                except:
                    server='unknown'
            try:
                host= row.get('Host').replace(' ','-')
            except:
                host=''
            while keyIter.hasNext():
                columnName = keyIter.next()
                value = row.get(columnName )
                if columnName.find('.value')>0:
                    metric_name=columnName.replace('.value','')
                    if value is not None: 
                        if outputFormat=='InfluxDB':
                            influx_msg= ('%s,server=%s,host=%s,metric_group=%s,metric_instance=%s value=%s %s') % (metric_name,server,host,tableName,inst_name,  value,now_epoch*1000000)
                            influx_msgs+='\n%s' % influx_msg
                            conn = httplib.HTTPConnection('%s:%s' % (targetHost,targetPort))
                            ## TODO pretty sure should be urlencoding this ...
                            a=conn.request("POST", ("/write?db=%s" % targetDB), influx_msg)
                            r=conn.getresponse()
                            if r.status != 204:
                                print 'Failed to send to InfluxDB! Error %s Reason %s' % (r.status,r.reason)
                                print influx_msg
                                #sys.exit(2)
                    else:
                        print 'Skipping None value %s,server=%s,host=%s,metric_group=%s,metric_instance=%s value=%s %s' % (metric_name,server,host,tableName,inst_name,  value,now_epoch*1000000)

我试过使用 While 循环,但这只是阻止了代码退出而不是重新循环

我想实现的是无限循环post连接到weblogic 即在这一行之后

connect(wls_user,wls_pw,url)

也许在重新运行

之前睡 5 秒

我们将不胜感激任何帮助

谢谢 P

您可以使用这种循环条件:

mainLoop = 'true'
while mainLoop == 'true' :  

这是迭代之间的暂停:

java.lang.Thread.sleep(3 * 1000)