Fetch 200 Ok status after running Curl Command and using that status write a condition using python in RUNDECK

独自空忆成欢 提交于 2021-01-29 15:19:51

问题


Below is the CURL command which gives the status running in RUNDECK

url = "curl -kv https://vn2-lpgdmt-capp99.rno.vzon.com:8990/health/check"

My Code is as follows:

payload={}
Headers={ i have defined here }

response = requests.request("GET", url, headers=headers, data = payload,verify=False)
status = response.status_code
print(status)
response_val = response.json()
response_val = json.dumps(response_val)

if status != 200  :
    print('********Error in Response***********')
    print('Status   :'+ str(status))
    print('********Please check for Rundeck Options,URL*********')
    print(response.content)
else:
    for pattern in part:
        if re.search(pattern,  response_val):
            print('Data Found for ServerNo :'+ pattern)
        else:
            print('No Data Found for ServerNo :'+ pattern)
print('**************************************************************************')

i want to give multiple curl commands and check status of all those servers , if status is 200 ok then send msg saying Heartbeat Good, if not Hearbeat is Dead" Error

kindly help


回答1:


You can generate a data value in the first step (on bash) and pick it up on the second step (on python), I leave a job definition example:

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='node' value='myhost' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>145d3f4e-44fc-4e25-9759-bf9d8867ef20</id>
    <loglevel>INFO</loglevel>
    <name>DataPassingDemo</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <description>get the code and save it on data variable</description>
        <fileExtension>.sh</fileExtension>
        <plugins>
          <LogFilter type='key-value-data'>
            <config>
              <invalidKeyPattern>\s|\$|\{|\}|\\</invalidKeyPattern>
              <logData>true</logData>
              <regex>^(CODE)\s*=\s*(.+)$</regex>
            </config>
          </LogFilter>
        </plugins>
        <script><![CDATA[response=$(curl --write-out %{http_code} --silent --output /dev/null http://@option.node@:4440)
echo "CODE=$response"]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
      <command>
        <description>your python 3 logic</description>
        <fileExtension>.py</fileExtension>
        <script><![CDATA[# get the data value
print("The code is: " + str(@data.CODE@))

# and you can do anything with @job.threadcount@
if (@data.CODE@ == 302):
    print ("Server OK")
else:
    print ("Check the instance")]]></script>
        <scriptargs />
        <scriptinterpreter>python3</scriptinterpreter>
      </command>
    </sequence>
    <uuid>145d3f4e-44fc-4e25-9759-bf9d8867ef20</uuid>
  </job>
</joblist>

Another way is to leave all processes to your python script through urllib, take a look at this.

You can check a good data passing explanation here.



来源:https://stackoverflow.com/questions/61952749/fetch-200-ok-status-after-running-curl-command-and-using-that-status-write-a-con

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