无限运行Jython Weblogic Sc​​ript

时间:2017-03-21 11:42:17

标签: python jython wlst

以下脚本是

的摘录

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循环,但这只是停止了代码退出而不是重新循环

我想要实现的是在连接到weblogic后无限循环 即在此行之后

connect(wls_user,wls_pw,url)

并且可能在重新跑步前睡5秒钟

任何和所有帮助将不胜感激

由于 P

1 个答案:

答案 0 :(得分:0)

你可以在循环中使用这种条件:

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

这是迭代之间的暂停:

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