Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Alerting Examples

textractor edited this page Dec 7, 2018 · 10 revisions

Examples


You can create and manage alerts using the Argus UI, the Argus SDK, or the Argus web service.

Argus Web

Existing alerts are listed in the alerts table. To add a new alert, click the New button on the upper-left of the table.

Once you set the alert's name, expression to evaluate, frequency of evaluation (see Quartz Scheduler for the format), and enabled the alert, create one or more triggers.

Finally, create a notification and associate it with one or more of the existing triggers. Notifications that are not associated with at least one trigger can't be sent. This example shows an email notification.

Make sure the alert is enabled before saving it.

Argus SDK

This simple example illustrates how to create a new alert, trigger and notifier. Alerts can have one or more notifiers and one or more triggers with a many to many mapping between the two. Take note of the call to the linkTrigger method.

int maxConnections = 10;
try (
    ArgusService service = ArgusService.getInstance(
      "https://argus.mycompany.com/argusws",
      maxConnections)
) {
    service.getAuthService().login("aUsername", "aPassword");
    Alert alert = new Alert();
    alert.setName("TestName");
    alert.setCronEntry("*/15 * * * *");
    alert.setExpression("-1d:TestScope:TestMetric:TestNamespace{TestTag=TagValue}:sum");
    alert = service.getAlertService().createAlert(alert);
    Trigger trigger = new Trigger();
    trigger.setName("TestName");
    trigger.setInertia(300000L);
    trigger.setThreshold(100.0);
    trigger.setType("GREATER_THAN");
    trigger = service.getAlertService().createTrigger(alert.getId(), trigger).get(0);
    Notification notifier = new Notification();
    notifier.setCooldownExpiration(3600000L);
    notifier.setNotifierName("com.salesforce.dva.argus.service.alert.notifier.EmailNotifier");
    notifier.setName("TestName");
    notifier.setSubscriptions(Arrays.asList(new String[]{"user@mycompany.com"}));
    try {
        notifier = service.getAlertService().createNotification(alert.getId(), notifier).get(0);
        service.getAlertService().linkTrigger(alert.getId(), notifier.getId(), trigger.getId());
        service.getAuthService().logout();
        } catch(TokenExpiredException e) {
	    try {
                // Looks like my access token has expired.
                // So I will obtain a new access token using refresh token.
	        service.getAuthService().obtainNewAccessToken();
	    } catch(TokenExpiredException e1) {
                // Looks like the refresh token itself has expired.
                // So I will re-login to Argus using username and password.
		service.getAuthService().login("aUsername", "aPassword");
	    } catch (IOException e1) {
                // Handle IOException as you would normally do.
        	e1.printStackTrace();
    	    }
        } catch (IOException e) {
            // Handle IOException as you would normally do.
            e.printStackTrace();
        }
}

Curl

This simple example uses curl to create an alert, trigger and notification.

#!/bin/bash
BASEURL=https://argus.mycompany.com/argusws/v2

# Get the refreshToken and accessToken
curl -X POST -H "Content-Type: application/json" \
     -d '{"username":"aUsername","password":"aPassword"}' \
     https://argus.mycompany.com/argusws/v2/auth/login \
     -o tokens.out

# You can use any utility of your choice to extract the accessToken 
# from the tokens.out file. 

###################
# Create the alert
###################

# Define an alert:
alertJS="{
  \"name\": \"TestAlert\",
  \"expression\": \"-10m:TestScope:TestMetric:min\",
  \"cronEntry\": \"*/5 * * * *\",
  \"enabled\": false,
  \"missingDataNotificationEnabled\": true
}"

# Define a command to create the alert and get its ID:
ALERT=`curl -H "Authorization: Bearer <accessToken from tokens.out>" \
    -H "Content-Type: application/json" \
    -X POST -d "$alertJS" \
    $BASEURL/alerts`
ALERTID=`echo $ALERT | sed -n 's/{"id":\([[:digit:]]*\),.*/\1/'p`

###################
# Create a trigger
###################

# Define a trigger:
triggerJS="{
  \"type\": \"NOT_EQUAL\",
  \"name\": \"TestTrigger\",
  \"threshold\": 1,
  \"inertia\": 0,
  \"alertId\" : $ALERTID
}"

# Define a command to create the trigger and get its ID:
TRIGGER=`curl -H "Authorization: Bearer <accessToken from tokens.out>" \
    -H "Content-Type: application/json" \
    -X POST -d "$triggerJS" \
    $BASEURL/alerts/$ALERTID/triggers`
TRIGGERID=`echo $TRIGGER | sed -n 's/\[{"id":\([[:digit:]]*\),.*/\1/'p`

###################
# Create a notification
###################

# Define a notification:
notificationJS="{
  \"name\": \"TestNotificaiton\",
  \"notifierName\": \"com.salesforce.dva.argus.service.alert.notifier.EmailNotifier\",
  \"subscriptions\": [\"me@mycompany.com\"],
  \"metricsToAnnotate\": [],
  \"cooldownPeriod\": 300000,
  \"alertId\" : $ALERTID
}"

# Define a command to create the notification and get its ID:
NOTIFICATION=`curl -H "Authorization: Bearer <accessToken from tokens.out>" \
    -H "Content-Type: application/json" \
    -X POST -d "$notificationJS" \
    $BASEURL/alerts/$ALERTID/notifications`
NOTIFICATIONID=`echo $NOTIFICATION | sed -n 's/\[{"id":\([[:digit:]]*\),.*/\1/'p`

###################
# Link the notification to the trigger
###################
curl -H "Authorization: Bearer <accessToken from tokens.out>" \
    -H "Content-Type: application/json" \
    -X POST \
    $BASEURL/alerts/$ALERTID/notifications/$NOTIFICATIONID/triggers/$TRIGGERID

curl -H "Authorization: Bearer <accessToken from tokens.out>" $BASEURL/auth/logout

For more information on authentication tokens, see Authentication.

If you are interested in using Python to interact with Argus, see the Argus Python client library.

Clone this wiki locally