solarwinds / OrionSDK

SDK for the SolarWinds Orion platform, including tools, documentation, and samples in PowerShell, C#, Go, Perl, and Java.
https://thwack.com/OrionSDK
Apache License 2.0
401 stars 143 forks source link

Integrate API to Solarwinds to send SMS Alert once Node goes Down #141

Closed harshaabba closed 6 years ago

harshaabba commented 6 years ago

Hi All,

I need to trigger an SMS Alert once the priority node goes down. I have received a API from a vendor called 'Whispir'. There are two ways to achieve this

  1. Using json

HTTP 1.1 POST https://api.whispir.com/messages?apikey=[your_key] Authorization: Basic am9obi5zbWl0aDpteXBhc3N3b3Jk

Content-Type: application/vnd.whispir.message-v1+json Accept: application/vnd.whispir.message-v1+json

{ "to" : "+1000000000", "subject" : "Test SMS Message", "body" : "This is the body of my test SMS message" }

  1. using xml

HTTP 1.1 POST https://api.whispir.com/messages?apikey=[your_key] Authorization: Basic am9obi5zbWl0aDpteXBhc3N3b3Jk

Content-Type: application/vnd.whispir.message-v1+xml Accept: application/vnd.whispir.message-v1+xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

+1000000000 Test SMS Message This is the body of my test SMS message Response: 202 Accepted Location: https://api.whispir.com/messages/{id}?apikey=[your_key] what method do I need to select in trigger action 1. send a get or post request to a web server or 2. execute an external program Please provide me an example and help me to achieve this
mrxinu commented 6 years ago

@tdanner can chime in here, but I think the issue you're going to run into is the inability to change the content-type from key/value pairs when using the "GET or POST" alert action. To make changes to the headers you're going to have to use the "External Executable" alert action and build the POST request yourself.

I'm happy to prototype something if you tell me what language you prefer.

harshaabba commented 6 years ago

Thanks a lot mrxinu for the quick response. you can use whatever language prefer you and easier for you. do I need to execute a external script for this to achieve ? I really appreciate your help in this regard. Please let me know if you need more information. I have blank knowledge integrating API to solar winds. Thanks a lot for the support. I highly value it

mrxinu commented 6 years ago

Written in Go:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    username := "someuser"
    password := "somepassword"
    apiKey := "somekeyhere"

    // build the URL
    uri := fmt.Sprintf("https://api.whispir.com/messages?apikey=%s", apiKey)

    // build the request structure
    rawBody := struct {
        To      string `json:"to"`
        Subject string `json:"subject"`
        Body    string `json:"body"`
    }{
        To:      "+1000000000",
        Subject: "Test SMS Message",
        Body:    "This is the body of my test SMS message",
    }

    // convert that structure into a JSON request
    body, err := json.Marshal(rawBody)
    if err != nil {
        log.Fatal(err)
    }

    // build the POST the request with the headers set appropriately
    req, err := http.NewRequest("POST", uri, bytes.NewReader(body))
    req.Header.Add("Content-Type", "application/vnd.whispir.message-v1+json")
    req.Header.Add("Accept", "application/vnd.whispir.message-v1+json")
    req.SetBasicAuth(username, password)

    // submit the request
    res, err := http.DefaultClient.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    // read the body of the results
    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        log.Fatal(err)
    }

    // print the body of the results
    fmt.Println(string(content))
}
harshaabba commented 6 years ago

@mrxinu , Thanks a lot for the script. I highly value it. I have some clarification to make.

  1. What extension I should use to save this script. Is it .py ?
  2. To the message can I include the node name and the ip address as follows ?

Body: "${N=SwisEntity;M=DisplayName} : ${N=SwisEntity;M=IP_Address} is Down",

Thanks again for the response. Can you please help me with this as well. Since I have fair knowledge scripting I highly value your response.

Thannks

mrxinu commented 6 years ago

Hey @harshaabba,

Would you mind creating a thread on the SolarWinds user forum so we can discuss it there? This isn't really the place for it and I think we might have a few iterations before it does quite what you want.

The area of the SolarWinds forum you want is here: https://thwack.solarwinds.com/community/labs/orion-sdk

Thanks in advance!