Knowledge Base

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I enable or disable synthetic jobs programmatically using the API?

Problem 

I want to enable/disable synthetic jobs programmatically in order to automate the process during the planned downtimes so that false alerts are not generated.

 

Solution

For dealing with synthetic jobs, use the https://api.eum-appdynamics.com endpoint to make changes to the schedule, and either enable or disable jobs.

 

To use this API, use your username and password for authentication. The username should be the EUM account name, and the password should be the EUM license key. These can be found on the licensing page in the controller UI by clicking on the gear menu in the upper right corner of the controller UI, clicking on license and then scrolling down to "End User Monitoring" as seen in the screenshot below. 

 

 

Screen Shot 2017-03-22 at 5.13.15 PM.png

 

 

 

The APIs below can be used to get and modify the schedules:  

Get a list of all the synthetic jobs available for the authenticated user:

 

  1. GET https://api.eum-appdynamics.com/v1/synthetic/schedule

 

Get the exact job identified by the schedule ID. To find the schedule ID and description, use the previous request to get a list of all jobs:

 

  1. GET https://api.eum-appdynamics.com/v1/synthetic/schedule/<schedule_id>

 

Modify the schedule according to the new schedule object provided in the body:

 

  1. PUT https://api.eum-appdynamics.com/v1/synthetic/schedule/<schedule_id>
    Use the response body from the previous request to get the exact job using the schedule ID as the JSON body. The field "userEnabled" controls whether the job is enabled or disabled, and will show the same result as enabling/disabling from the UI.

 

Sample curl implementation: 

Get all schedules:

 

curl -XGET --user AppDynamics-a0Q340ABCDEABCDEAV:9ba9bd3b-652d-abcd-abcd-abcdefecd4f0 https://api.eum-appdynamics.com/v1/synthetic/schedule
Output:
{
    "_first": null,
    "_items": [
        {
            "_id": "fcedd1d0-88bc-49c1-9bbe-397227616d1f",
            "appKey": "AD-AAB-WWW-WWW",
            "browserCodes": [
                "Chrome"
            ],
            "captureVisualMetrics": true,
            "created": "2016-11-02T04:07:22.764Z",
.....

 

Get a particular schedule:

 

curl -XGET --user AppDynamics-a0Q340ABCDEABCDEAV:9ba9bd3b-652d-abcd-abcd-abcdefecd4f0 https://api.eum-appdynamics.com/v1/synthetic/schedule/fcedd1d0-88bc-49c1-9bbe-397227616d1f 2>/dev/null|python -m json.tool
{
    "_id": "fcedd1d0-88bc-49c1-9bbe-397227616d1f",
    .....
    "userEnabled": false,
    "version": 13
}

 

Modify the given schedule:

 

curl -XPUT --user AppDynamics-a0Q340ABCDEABCDEAV:9ba9bd3b-652d-abcd-abcd-abcdefecd4f0 -H"Content-Type: application/json" -d '{
    "_id": "fcedd1d0-88bc-49c1-9bbe-397227616d1f",
    .....
    "userEnabled": true,
    "version": 13
}' https://api.eum-appdynamics.com/v1/synthetic/schedule/fcedd1d0-88bc-49c1-9bbe-397227616d1f

 

 

Use the following Python implementation as a starting point to create your own script.

 

osxltmkshi:analytics-agent mayuresh.kshirsagar$ python ~/Desktop/synth.py --help
usage: synth.py [-h] -n <eum_account_name> -l <eum_license_name>
                [-u <eum_url>] -j <synth_job_name> [-e]

optional arguments:
  -h, --help            show this help message and exit
  -n <eum_account_name>, --eumaccountname <eum_account_name>
                        EUM Account Name
  -l <eum_license_name>, --eumlicensekey <eum_license_name>
                        EUM License Key
  -u <eum_url>, --url <eum_url>
                        EUM Server URL, Defaults to https://api.eum-
                        appdynamics.com
  -j <synth_job_name>, --job <synth_job_name>
                        Job Name
  -e, --enable          Enable Job - If present marks the job enabled. If
                        absent, marks the job disabled

 

Enable a particular job:

 

python ~/Desktop/synth.py -n AppDynamics-a0Q340ABCDEABCDEAV -l 9ba9bd3b-652d-abcd-abcd-abcdefecd4f0 -j 90220 -e

 

Disable a particular job:

 

python ~/Desktop/synth.py -n AppDynamics-a0Q340ABCDEABCDEAV -l 9ba9bd3b-652d-abcd-abcd-abcdefecd4f0 -j 90220

 

 

 

Related Links:

 

synth.py:

 

# -*- coding: utf-8 -*-

import requests
import argparse
import sys
import json

def find(list, filter):
    for x in list:
        if filter(x):
            return x
    return None

def main():
    accountname=None
    licensekey=None
    url=None
    jobname=None
    enable=False
    getallschedules="/v1/synthetic/schedule"
    scheduleendpoint="/v1/synthetic/schedule/"
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-n", "--eumaccountname", help="EUM Account Name", required=True, metavar='<eum_account_name>')
    parser.add_argument("-l", "--eumlicensekey", help="EUM License Key", required=True, metavar='<eum_license_name>')
    parser.add_argument("-u", "--url", help="EUM Server URL, Defaults to https://api.eum-appdynamics.com", default="https://api.eum-appdynamics.com", required=False, metavar='<eum_url>')
    parser.add_argument("-j", "--job", help="Job Name", required=True, metavar='<synth_job_name>')
    parser.add_argument("-e", "--enable", help="Enable Job - If present marks the job enabled. If absent, marks the job disabled", default=False, action="store_true", required=False)
    args = parser.parse_args()
    
    accountname=args.eumaccountname
    licensekey=args.eumlicensekey
    url=args.url
    jobname=args.job
    enable=args.enable
    
    # Get all the schedules
    response = requests.get(url + getallschedules,auth=(accountname,licensekey))
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)
        
    _json = json.loads(response.text)
    _items= _json['_items']
    _return = find(_items, lambda x: x['description'] == jobname)
    scheduleid=_return['_id']
    
    # Get the individual schedule matching the description
    response = requests.get(url + scheduleendpoint + scheduleid,auth=(accountname, licensekey))
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)

    _json = json.loads(response.text)
    _json['userEnabled'] = enable
    _json=json.dumps(_json, indent=3)
    print "Request: " + _json
    headers = {'Content-type': 'application/json'}

    # Modify the schedule
    response = requests.put(url + scheduleendpoint + scheduleid, auth=(accountname, licensekey), headers=headers, data=_json)
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)

    _json = json.loads(response.text)
    _json=json.dumps(_json, indent=3)
    print "Response: " + _json


if __name__ == "__main__":
    main()

 

 

Version history
Revision #:
12 of 12
Last update:
‎10-12-2018 02:20 PM
Updated by:
 
Labels (1)
0 Kudos
Comments
alakshya.maheria

Hi Mayuresh,

 

Great article.

Unfortunately I could not see a link to download the synth.py file. Could you please help out.

 

Thanks,

Alakshya

Erin

Hi Alakshya,

 

That's strange, the script does not show up in the published article but it's there when I click edit. I'll check with the team about that.

 

 

Anyway, here is the script:

 

synth.py:

 

# -*- coding: utf-8 -*-

import requests
import argparse
import sys
import json

def find(list, filter):
    for x in list:
        if filter(x):
            return x
    return None

def main():
    accountname=None
    licensekey=None
    url=None
    jobname=None
    enable=False
    getallschedules="/v1/synthetic/schedule"
    scheduleendpoint="/v1/synthetic/schedule/"
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-n", "--eumaccountname", help="EUM Account Name", required=True, metavar='<eum_account_name>')
    parser.add_argument("-l", "--eumlicensekey", help="EUM License Key", required=True, metavar='<eum_license_name>')
    parser.add_argument("-u", "--url", help="EUM Server URL, Defaults to https://api.eum-appdynamics.com", default="https://api.eum-appdynamics.com", required=False, metavar='<eum_url>')
    parser.add_argument("-j", "--job", help="Job Name", required=True, metavar='<synth_job_name>')
    parser.add_argument("-e", "--enable", help="Enable Job - If present marks the job enabled. If absent, marks the job disabled", default=False, action="store_true", required=False)
    args = parser.parse_args()
    
    accountname=args.eumaccountname
    licensekey=args.eumlicensekey
    url=args.url
    jobname=args.job
    enable=args.enable
    
    # Get all the schedules
    response = requests.get(url + getallschedules,auth=(accountname,licensekey))
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)
        
    _json = json.loads(response.text)
    _items= _json['_items']
    _return = find(_items, lambda x: x['description'] == jobname)
    scheduleid=_return['_id']
    
    # Get the individual schedule matching the description
    response = requests.get(url + scheduleendpoint + scheduleid,auth=(accountname, licensekey))
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)

    _json = json.loads(response.text)
    _json['userEnabled'] = enable
    _json=json.dumps(_json, indent=3)
    print "Request: " + _json
    headers = {'Content-type': 'application/json'}

    # Modify the schedule
    response = requests.put(url + scheduleendpoint + scheduleid, auth=(accountname, licensekey), headers=headers, data=_json)
    if response.status_code != 200:
        print "Error Occurred. Status Code: " + str(response.status_code) + " Response: " + json.dumps(json.loads(response.text),indent=3)
        sys.exit(1)

    _json = json.loads(response.text)
    _json=json.dumps(_json, indent=3)
    print "Response: " + _json


if __name__ == "__main__":
    main()

 

alakshya.maheria
Thanks Erin. Regards, Alakshya
alakshya.maheria

Hi Erin/Mayuresh,

 

Is there an option to delete jobs using rest call.

 

Thanks,

Alakshya

alakshya.maheria

Got it. The below worked.

curl -XDELETE -x <http proxy>:<proxy port> --user <EumAccountName>:<EUMKey> https://api.eumappdynamics.com/v1/synthetic/schedule/<id>