[Tutor] request.post in If condition

sourav voip voipdev.sourav at gmail.com
Mon Oct 30 06:42:27 EDT 2017


Hi All,

I'm trying to hit request.post with condition using if-else as below...
I;m posting the full script here...as I've tried declaring post url details
tested with multiple places..

If condition for disk utiliztion is working perfect ,however request.post
is not hitting.
Please suggest any hint/clue as I'm a new learner in python.

Regards,
Sourav

------------------------------------------------------------
-------------------------
#!/usr/bin/python
import re,sys,commands
import requests
# Set the request parameters
url = 'https://devxxxx.service-now.com/api/now/table/incident'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'xxxx'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}


#################
#Set variables
command = "df /"
critical = 50.0
warning = 40.0
#################

#build regex
dfPattern = re.compile('[0-9]+')

#get disk utilization
diskUtil = commands.getstatusoutput(command)

#split out the util %
diskUtil = diskUtil[1].split()[11]

#look for a match. If no match exit and return an
#UNKNOWN (3) state to Nagios

matchobj = dfPattern.match(diskUtil)
if (matchobj):
    diskUtil = eval(matchobj.group(0))
else:
    print "STATE UNKNOWN"
    sys.exit(3)

#Determine state to pass to Nagios
#CRITICAL = 2
#WARNING = 1
#OK = 0
if diskUtil >= critical:
    url = 'https://devxxxx.service-now.com/api/now/table/incident'
    user = 'admin'
    pwd = 'xxxx'
    headers = {"Content-Type":"application/json","Accept":"application/
json"}
    requests.post(url, auth=(user, pwd), headers=headers
,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold
critical\"}")
    print "FREE SPACE CRITICAL: '/' is %.2f%% full" % (float(diskUtil))
    sys.exit(2)
elif diskUtil >= warning:
    requests.post(url, auth=(user, pwd), headers=headers
,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold
Warning\"}")
    print "FREE SPACE WARNING: '/' is %.2f%% full" % (float(diskUtil))
    sys.exit(1)
else:
    print "FREE SPACE OK: '/' is %.2f%% full" % (float(diskUtil))
    sys.exit(0)


On Mon, Oct 30, 2017 at 4:04 PM, sourav voip <voipdev.sourav at gmail.com>
wrote:

> Hi All,
>
> I'm trying to hit request.post with condition using if-else as below...
> I;m posting the full script here...as I've tried declaring post url
> details tested with multiple places.
>
>
>
>
>


More information about the Tutor mailing list