Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "flag"
- "fmt"
- "log"
- "os"
- "time"
- "encoding/json"
- "github.com/mrxinu/gosolar"
- )
- func main() {
- // get the command line arguments
- hostname := flag.String("h", "", "hostname")
- username := flag.String("u", "", "username")
- password := flag.String("p", "", "password")
- filename := flag.String("f", "", "input filename")
- flag.Parse()
- // check to make sure none of them are empty
- if *hostname == "" || *username == "" || *password == "" {
- fmt.Println(usage())
- os.Exit(0)
- }
- // read the addresses out of the file
- addresses, err := readFile(*filename)
- if err != nil {
- log.Fatal(err)
- }
- // print out the total host count
- fmt.Printf("Total host count is %v\n", len(addresses))
- // get the current time for use in the message
- now := time.Now().String()
- // create a new client to talk to SolarWinds
- c := gosolar.NewClient(*hostname, *username, *password, true)
- // iterate over the addresses found in the file
- for _, a := range addresses {
- // build a parameter map to use in the query
- parameters := map[string]string{
- "ipAddress": a,
- }
- // define the query
- query := "SELECT Caption, URI FROM Orion.Nodes WHERE IPAddress = @ipAddress"
- // define the structure for the results
- nodes := []struct {
- Caption string `json:"caption"`
- URI string `json:"uri"`
- }{}
- // get the results from the query
- res, err := c.Query(query, parameters)
- if err != nil {
- log.Fatal(err)
- }
- // convert the results from json to our struct
- if err := json.Unmarshal(res, &nodes); err != nil {
- log.Fatal(err)
- }
- // there should be only one match to an IP address, so this should only
- // cycle through one time, but if there were more, it would still work
- for _, n := range nodes {
- fmt.Printf("Setting custom property value on node %s...", n.Caption)
- // build the custom property value
- value := fmt.Sprintf("Unmanaged via Automation by %v - %v", *username, now)
- // set the custom property on the node
- if err := c.SetCustomProperty(n.URI, "Comment", value); err != nil {
- log.Fatalf("failed to set custom property: %v", err)
- }
- // if we get this far, it worked
- fmt.Printf(" SUCCESS!\n")
- }
- }
- }
- func usage() string {
- return fmt.Sprintf("Usage: %s -h <host> -u <user> -p <pass> -f <inputfile>", os.Args[0])
- }
- func readFile(filename string) ([]string, error) {
- file, err := os.Open(filename)
- if err != nil {
- return nil, fmt.Errorf("failed to open input file: %v", err)
- }
- defer file.Close()
- scanner := bufio.NewScanner(file)
- var addresses []string
- for scanner.Scan() {
- line := scanner.Text()
- if line != "" {
- addresses = append(addresses, scanner.Text())
- }
- }
- if err := scanner.Err(); err != nil {
- return nil, fmt.Errorf("failed to read line from file: %v", err)
- }
- return addresses, nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement