Advertisement
Guest User

Untitled

a guest
May 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "flag"
  6. "fmt"
  7. "log"
  8. "os"
  9. "time"
  10.  
  11. "encoding/json"
  12.  
  13. "github.com/mrxinu/gosolar"
  14. )
  15.  
  16. func main() {
  17. // get the command line arguments
  18. hostname := flag.String("h", "", "hostname")
  19. username := flag.String("u", "", "username")
  20. password := flag.String("p", "", "password")
  21. filename := flag.String("f", "", "input filename")
  22. flag.Parse()
  23.  
  24. // check to make sure none of them are empty
  25. if *hostname == "" || *username == "" || *password == "" {
  26. fmt.Println(usage())
  27. os.Exit(0)
  28. }
  29.  
  30. // read the addresses out of the file
  31. addresses, err := readFile(*filename)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35.  
  36. // print out the total host count
  37. fmt.Printf("Total host count is %v\n", len(addresses))
  38.  
  39. // get the current time for use in the message
  40. now := time.Now().String()
  41.  
  42. // create a new client to talk to SolarWinds
  43. c := gosolar.NewClient(*hostname, *username, *password, true)
  44.  
  45. // iterate over the addresses found in the file
  46. for _, a := range addresses {
  47. // build a parameter map to use in the query
  48. parameters := map[string]string{
  49. "ipAddress": a,
  50. }
  51.  
  52. // define the query
  53. query := "SELECT Caption, URI FROM Orion.Nodes WHERE IPAddress = @ipAddress"
  54.  
  55. // define the structure for the results
  56. nodes := []struct {
  57. Caption string `json:"caption"`
  58. URI string `json:"uri"`
  59. }{}
  60.  
  61. // get the results from the query
  62. res, err := c.Query(query, parameters)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66.  
  67. // convert the results from json to our struct
  68. if err := json.Unmarshal(res, &nodes); err != nil {
  69. log.Fatal(err)
  70. }
  71.  
  72. // there should be only one match to an IP address, so this should only
  73. // cycle through one time, but if there were more, it would still work
  74. for _, n := range nodes {
  75. fmt.Printf("Setting custom property value on node %s...", n.Caption)
  76.  
  77. // build the custom property value
  78. value := fmt.Sprintf("Unmanaged via Automation by %v - %v", *username, now)
  79.  
  80. // set the custom property on the node
  81. if err := c.SetCustomProperty(n.URI, "Comment", value); err != nil {
  82. log.Fatalf("failed to set custom property: %v", err)
  83. }
  84.  
  85. // if we get this far, it worked
  86. fmt.Printf(" SUCCESS!\n")
  87. }
  88. }
  89. }
  90.  
  91. func usage() string {
  92. return fmt.Sprintf("Usage: %s -h <host> -u <user> -p <pass> -f <inputfile>", os.Args[0])
  93. }
  94.  
  95. func readFile(filename string) ([]string, error) {
  96. file, err := os.Open(filename)
  97. if err != nil {
  98. return nil, fmt.Errorf("failed to open input file: %v", err)
  99. }
  100. defer file.Close()
  101.  
  102. scanner := bufio.NewScanner(file)
  103.  
  104. var addresses []string
  105. for scanner.Scan() {
  106. line := scanner.Text()
  107.  
  108. if line != "" {
  109. addresses = append(addresses, scanner.Text())
  110. }
  111. }
  112.  
  113. if err := scanner.Err(); err != nil {
  114. return nil, fmt.Errorf("failed to read line from file: %v", err)
  115. }
  116.  
  117. return addresses, nil
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement