|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/equelin/gounity" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +var log = logrus.New() |
| 15 | +var unityName string |
| 16 | + |
| 17 | +// https://stackoverflow.com/questions/29366038/looping-iterate-over-the-second-level-nested-json-in-go-lang |
| 18 | +func parseMap(index int, pathPtr *string, measurementNamePtr *string, tagNames map[int]string, tagsMap map[string]string, valuesMap map[string]interface{}) { |
| 19 | + |
| 20 | + for key, val := range valuesMap { |
| 21 | + |
| 22 | + pathSplit := strings.Split(*pathPtr, ".") |
| 23 | + |
| 24 | + switch concreteVal := val.(type) { |
| 25 | + case map[string]interface{}: |
| 26 | + |
| 27 | + ok := false |
| 28 | + |
| 29 | + for i, v := range pathSplit { |
| 30 | + if v == key { |
| 31 | + tagName := pathSplit[i-1] |
| 32 | + tagsMap[tagName] = key |
| 33 | + ok = true |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if ok != true { |
| 38 | + tagsMap[tagNames[index]] = key |
| 39 | + index++ |
| 40 | + } |
| 41 | + |
| 42 | + parseMap( |
| 43 | + index, |
| 44 | + pathPtr, |
| 45 | + measurementNamePtr, |
| 46 | + tagNames, |
| 47 | + tagsMap, |
| 48 | + val.(map[string]interface{}), |
| 49 | + ) |
| 50 | + |
| 51 | + default: |
| 52 | + |
| 53 | + if len(tagNames) != 0 { |
| 54 | + tagsMap[tagNames[index]] = key |
| 55 | + } else { |
| 56 | + for i, v := range pathSplit { |
| 57 | + if v == key { |
| 58 | + tagName := pathSplit[i-1] |
| 59 | + tagsMap[tagName] = key |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Formating tags set |
| 65 | + // <tag_key>=<tag_value>,<tag_key>=<tag_value> |
| 66 | + var tags string |
| 67 | + |
| 68 | + tags = fmt.Sprintf("unity=%s", unityName) |
| 69 | + for k, v := range tagsMap { |
| 70 | + tags = tags + fmt.Sprintf(",%s=%s", k, v) |
| 71 | + } |
| 72 | + |
| 73 | + // Formating fied set |
| 74 | + // <field_key>=<field_value> |
| 75 | + var field string |
| 76 | + field = fmt.Sprintf("%s=%s", *pathPtr, concreteVal) |
| 77 | + |
| 78 | + // Formating and printing the result using the InfluxDB's Line Protocol |
| 79 | + // https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_tutorial/ |
| 80 | + fmt.Printf("%s,%s %s %d\n", *measurementNamePtr, tags, field, time.Now().UnixNano()) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func main() { |
| 86 | + |
| 87 | + // Set logs parameters |
| 88 | + log.Out = os.Stdout |
| 89 | + |
| 90 | + userPtr := flag.String("user", "", "Username") |
| 91 | + passwordPtr := flag.String("password", "", "Password") |
| 92 | + unityPtr := flag.String("unity", "", "Unity IP or FQDN") |
| 93 | + intervalPtr := flag.Uint64("interval", 30, "Sampling interval") |
| 94 | + pathsPtr := flag.String("paths", "kpi.sp.spa.utilization,sp.*.cpu.summary.busyTicks", "Unity metrics paths") |
| 95 | + debugPtr := flag.Bool("debug", false, "Debug mode") |
| 96 | + |
| 97 | + flag.Parse() |
| 98 | + |
| 99 | + if *debugPtr == true { |
| 100 | + log.Level = logrus.DebugLevel |
| 101 | + } else { |
| 102 | + log.Level = logrus.ErrorLevel |
| 103 | + } |
| 104 | + |
| 105 | + log.WithFields(logrus.Fields{ |
| 106 | + "event": "flag", |
| 107 | + "key": "user", |
| 108 | + "value": *userPtr, |
| 109 | + }).Debug("Parsed flag user") |
| 110 | + |
| 111 | + log.WithFields(logrus.Fields{ |
| 112 | + "event": "flag", |
| 113 | + "key": "unity", |
| 114 | + "value": *unityPtr, |
| 115 | + }).Debug("Parsed flag unity") |
| 116 | + |
| 117 | + log.WithFields(logrus.Fields{ |
| 118 | + "event": "flag", |
| 119 | + "key": "interval", |
| 120 | + "value": *intervalPtr, |
| 121 | + }).Debug("Parsed flag interval") |
| 122 | + |
| 123 | + log.WithFields(logrus.Fields{ |
| 124 | + "event": "flag", |
| 125 | + "key": "paths", |
| 126 | + "value": *pathsPtr, |
| 127 | + }).Debug("Parsed flag paths") |
| 128 | + |
| 129 | + // Start a new Unity session |
| 130 | + |
| 131 | + log.WithFields(logrus.Fields{ |
| 132 | + "event": "gounity.NewSession", |
| 133 | + "unity": *unityPtr, |
| 134 | + "engineering": "true", |
| 135 | + "user": *userPtr, |
| 136 | + }).Debug("Started new Unity session") |
| 137 | + |
| 138 | + session, err := gounity.NewSession(*unityPtr, true, *userPtr, *passwordPtr) |
| 139 | + |
| 140 | + if err != nil { |
| 141 | + log.Fatal(err) |
| 142 | + } |
| 143 | + |
| 144 | + defer session.CloseSession() |
| 145 | + |
| 146 | + // Get system informations |
| 147 | + System, err := session.GetbasicSystemInfo() |
| 148 | + if err != nil { |
| 149 | + log.Fatal(err) |
| 150 | + } |
| 151 | + |
| 152 | + // Store the name of the Unity |
| 153 | + unityName = System.Entries[0].Content.Name |
| 154 | + |
| 155 | + // metric paths |
| 156 | + paths := strings.Split(*pathsPtr, ",") |
| 157 | + |
| 158 | + // converting metric interval into uint32 |
| 159 | + var interval = uint32(*intervalPtr) |
| 160 | + |
| 161 | + // Request a new metric query |
| 162 | + Metric, err := session.NewMetricRealTimeQuery(paths, interval) |
| 163 | + if err != nil { |
| 164 | + log.Fatal(err) |
| 165 | + } |
| 166 | + |
| 167 | + // Waiting that the sampling of the metrics is done |
| 168 | + time.Sleep(time.Duration(Metric.Content.Interval) * time.Second) |
| 169 | + |
| 170 | + // Get the results of the query |
| 171 | + Result, err := session.GetMetricRealTimeQueryResult(Metric.Content.ID) |
| 172 | + if err != nil { |
| 173 | + log.Fatal(err) |
| 174 | + } |
| 175 | + |
| 176 | + // Parse the results |
| 177 | + for _, v := range Result.Entries { |
| 178 | + |
| 179 | + valuesMap := v.Content.Values.(map[string]interface{}) |
| 180 | + |
| 181 | + tagsMap := make(map[string]string) |
| 182 | + tagNames := make(map[int]string) |
| 183 | + |
| 184 | + path := v.Content.Path |
| 185 | + |
| 186 | + pathSplit := strings.Split(path, ".") |
| 187 | + |
| 188 | + var measurementName string |
| 189 | + if pathSplit[0] == "kpi" { |
| 190 | + measurementName = "kpi" |
| 191 | + } else { |
| 192 | + measurementName = pathSplit[2] |
| 193 | + } |
| 194 | + |
| 195 | + j := 0 |
| 196 | + for i, v := range pathSplit { |
| 197 | + if v == "*" { |
| 198 | + tagName := pathSplit[i-1] |
| 199 | + tagNames[j] = tagName |
| 200 | + j++ |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + parseMap( |
| 205 | + 0, |
| 206 | + &path, |
| 207 | + &measurementName, |
| 208 | + tagNames, |
| 209 | + tagsMap, |
| 210 | + valuesMap, |
| 211 | + ) |
| 212 | + } |
| 213 | + |
| 214 | + /* TODO: DELETE THE QUERY */ |
| 215 | + |
| 216 | +} |
0 commit comments