Skip to content

Instantly share code, notes, and snippets.

@mrxinu
Created May 24, 2017 23:40
Show Gist options
  • Save mrxinu/bf188c1b586fdb8d0d0a99956e5648e3 to your computer and use it in GitHub Desktop.
Save mrxinu/bf188c1b586fdb8d0d0a99956e5648e3 to your computer and use it in GitHub Desktop.
A rewrote a PowerShell script a colleague had written in Go so he could see how it compares. Of course I used my own package because why reinvent the wheel, right?
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment