func processResponse(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) { switch t := v.(type) { case map[string]interface{}: for k, v := range t { processResponse(acc, prefix+"_"+k, tags, v) } case float64: acc.Add(prefix, v, tags) } }
func (e *Engine) AddEngineStats(keys []string, acc plugins.Accumulator, tags map[string]string) { engine := reflect.ValueOf(e).Elem() for _, key := range keys { acc.Add( key, engine.FieldByName(engineStats[key]).Interface(), tags, ) } }
func (s *NetIOStats) Gather(acc plugins.Accumulator) error { netio, err := s.ps.NetIO() if err != nil { return fmt.Errorf("error getting net io info: %s", err) } for _, io := range netio { if len(s.Interfaces) != 0 { var found bool for _, name := range s.Interfaces { if name == io.Name { found = true break } } if !found { continue } } else if !s.skipChecks { iface, err := net.InterfaceByName(io.Name) if err != nil { continue } if iface.Flags&net.FlagLoopback == net.FlagLoopback { continue } if iface.Flags&net.FlagUp == 0 { continue } } tags := map[string]string{ "interface": io.Name, } acc.Add("bytes_sent", io.BytesSent, tags) acc.Add("bytes_recv", io.BytesRecv, tags) acc.Add("packets_sent", io.PacketsSent, tags) acc.Add("packets_recv", io.PacketsRecv, tags) acc.Add("err_in", io.Errin, tags) acc.Add("err_out", io.Errout, tags) acc.Add("drop_in", io.Dropin, tags) acc.Add("drop_out", io.Dropout, tags) } return nil }
func (l *Lustre2) GetLustreProcStats(fileglob string, wanted_fields []*mapping, acc plugins.Accumulator) error { files, err := filepath.Glob(fileglob) if err != nil { return err } for _, file := range files { /* Turn /proc/fs/lustre/obdfilter/<ost_name>/stats and similar * into just the object store target name * Assumpion: the target name is always second to last, * which is true in Lustre 2.1->2.5 */ path := strings.Split(file, "/") name := path[len(path)-2] tags := map[string]string{ "name": name, } lines, err := common.ReadLines(file) if err != nil { return err } for _, line := range lines { fields := strings.Fields(line) for _, wanted := range wanted_fields { var data uint64 if fields[0] == wanted.inProc { wanted_field := wanted.field // if not set, assume field[1]. Shouldn't be field[0], as // that's a string if wanted_field == 0 { wanted_field = 1 } data, err = strconv.ParseUint((fields[wanted_field]), 10, 64) if err != nil { return err } report_name := wanted.inProc if wanted.reportAs != "" { report_name = wanted.reportAs } acc.Add(report_name, data, tags) } } } } return nil }
func (s *SwapStats) Gather(acc plugins.Accumulator) error { swap, err := s.ps.SwapStat() if err != nil { return fmt.Errorf("error getting swap memory info: %s", err) } swaptags := map[string]string(nil) acc.Add("total", swap.Total, swaptags) acc.Add("used", swap.Used, swaptags) acc.Add("free", swap.Free, swaptags) acc.Add("used_perc", swap.UsedPercent, swaptags) acc.Add("in", swap.Sin, swaptags) acc.Add("out", swap.Sout, swaptags) return nil }
func (e *Elasticsearch) parseInterface(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) error { switch t := v.(type) { case map[string]interface{}: for k, v := range t { if err := e.parseInterface(acc, prefix+"_"+k, tags, v); err != nil { return err } } case float64: acc.Add(prefix, t, tags) case bool, string, []interface{}: // ignored types return nil default: return fmt.Errorf("elasticsearch: got unexpected type %T with value %v (%s)", t, t, prefix) } return nil }
func (_ *SystemStats) Gather(acc plugins.Accumulator) error { loadavg := sigar.LoadAverage{} if err := loadavg.Get(); err != nil { return err } uptime := sigar.Uptime{} if err := uptime.Get(); err != nil { return err } acc.Add("load1", loadavg.One, nil) acc.Add("load5", loadavg.Five, nil) acc.Add("load15", loadavg.Fifteen, nil) acc.Add("uptime", uptime.Length, nil) acc.Add("uptime_format", uptime.Format(), nil) return nil }
func emitMetrics(k *Kafka, acc plugins.Accumulator, metricConsumer <-chan []byte) error { timeout := time.After(1 * time.Second) for { select { case batch := <-metricConsumer: var points []tsdb.Point var err error if points, err = tsdb.ParsePoints(batch); err != nil { return err } for _, point := range points { acc.AddValuesWithTime(point.Name(), point.Fields(), point.Tags(), point.Time()) } case <-timeout: return nil } } }
func (p *Ping) Gather(acc plugins.Accumulator) error { var wg sync.WaitGroup errorChannel := make(chan error, len(p.Urls)*2) // Spin off a go routine for each url to ping for _, url := range p.Urls { wg.Add(1) go func(url string, acc plugins.Accumulator) { defer wg.Done() args := p.args(url) out, err := p.pingHost(args...) if err != nil { // Combine go err + stderr output errorChannel <- errors.New( strings.TrimSpace(out) + ", " + err.Error()) } tags := map[string]string{"url": url} trans, rec, avg, err := processPingOutput(out) if err != nil { // fatal error errorChannel <- err return } // Calculate packet loss percentage loss := float64(trans-rec) / float64(trans) * 100.0 acc.Add("packets_transmitted", trans, tags) acc.Add("packets_received", rec, tags) acc.Add("percent_packet_loss", loss, tags) acc.Add("average_response_ms", avg, tags) }(url, acc) } wg.Wait() close(errorChannel) // Get all errors and return them as one giant error errorStrings := []string{} for err := range errorChannel { errorStrings = append(errorStrings, err.Error()) } if len(errorStrings) == 0 { return nil } return errors.New(strings.Join(errorStrings, "\n")) }
func (l *LeoFS) gatherServer(endpoint string, serverType ServerType, acc plugins.Accumulator) error { cmd := exec.Command("snmpwalk", "-v2c", "-cpublic", endpoint, oid) stdout, err := cmd.StdoutPipe() if err != nil { return err } cmd.Start() defer cmd.Wait() scanner := bufio.NewScanner(stdout) if !scanner.Scan() { return fmt.Errorf("Unable to retrieve the node name") } nodeName, err := retrieveTokenAfterColon(scanner.Text()) if err != nil { return err } nodeNameTrimmed := strings.Trim(nodeName, "\"") tags := map[string]string{ "node": nodeNameTrimmed, } i := 0 for scanner.Scan() { key := KeyMapping[serverType][i] val, err := retrieveTokenAfterColon(scanner.Text()) if err != nil { return err } fVal, err := strconv.ParseFloat(val, 64) if err != nil { return fmt.Errorf("Unable to parse the value:%s, err:%s", val, err) } acc.Add(key, fVal, tags) i++ } return nil }
func (n *Apache) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { resp, err := client.Get(addr.String()) if err != nil { return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status) } tags := getTags(addr) sc := bufio.NewScanner(resp.Body) for sc.Scan() { line := sc.Text() if strings.Contains(line, ":") { parts := strings.SplitN(line, ":", 2) key, part := strings.Replace(parts[0], " ", "", -1), strings.TrimSpace(parts[1]) switch key { case "Scoreboard": n.gatherScores(part, acc, tags) default: value, err := strconv.ParseFloat(part, 32) if err != nil { continue } acc.Add(key, value, tags) } } } return nil }
func importCsvResult(r io.Reader, acc plugins.Accumulator, host string) ([][]string, error) { csv := csv.NewReader(r) result, err := csv.ReadAll() for _, row := range result { for field, v := range row { tags := map[string]string{ "host": host, "proxy": row[HF_PXNAME], "sv": row[HF_SVNAME], } switch field { case HF_QCUR: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("qcur", ival, tags) } case HF_QMAX: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("qmax", ival, tags) } case HF_SCUR: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("scur", ival, tags) } case HF_SMAX: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("smax", ival, tags) } case HF_BIN: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("bin", ival, tags) } case HF_BOUT: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("bout", ival, tags) } case HF_DREQ: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("dreq", ival, tags) } case HF_DRESP: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("dresp", ival, tags) } case HF_RATE: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("rate", ival, tags) } case HF_RATE_MAX: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("rate_max", ival, tags) } case HF_STOT: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("stot", ival, tags) } case HF_HRSP_1xx: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("http_response.1xx", ival, tags) } case HF_HRSP_2xx: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("http_response.2xx", ival, tags) } case HF_HRSP_3xx: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("http_response.3xx", ival, tags) } case HF_HRSP_4xx: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("http_response.4xx", ival, tags) } case HF_EREQ: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("ereq", ival, tags) } case HF_ERESP: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("eresp", ival, tags) } case HF_ECON: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("econ", ival, tags) } case HF_WRETR: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("wretr", ival, tags) } case HF_WREDIS: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("wredis", ival, tags) } case HF_REQ_RATE: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("req_rate", ival, tags) } case HF_REQ_RATE_MAX: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("req_rate_max", ival, tags) } case HF_REQ_TOT: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("req_tot", ival, tags) } case HF_THROTTLE: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("throttle", ival, tags) } case HF_LBTOT: ival, err := strconv.ParseUint(v, 10, 64) if err == nil { acc.Add("lbtot", ival, tags) } } } } return result, err }
func (s *MemStats) Gather(acc plugins.Accumulator) error { vm, err := s.ps.VMStat() if err != nil { return fmt.Errorf("error getting virtual memory info: %s", err) } vmtags := map[string]string(nil) acc.Add("total", vm.Total, vmtags) acc.Add("available", vm.Available, vmtags) acc.Add("used", vm.Used, vmtags) acc.Add("used_perc", vm.UsedPercent, vmtags) acc.Add("free", vm.Free, vmtags) acc.Add("active", vm.Active, vmtags) acc.Add("inactive", vm.Inactive, vmtags) acc.Add("buffers", vm.Buffers, vmtags) acc.Add("cached", vm.Cached, vmtags) acc.Add("wired", vm.Wired, vmtags) acc.Add("shared", vm.Shared, vmtags) return nil }
func (g *Disque) gatherServer(addr *url.URL, acc plugins.Accumulator) error { if g.c == nil { _, _, err := net.SplitHostPort(addr.Host) if err != nil { addr.Host = addr.Host + ":" + defaultPort } c, err := net.Dial("tcp", addr.Host) if err != nil { return fmt.Errorf("Unable to connect to disque server '%s': %s", addr.Host, err) } if addr.User != nil { pwd, set := addr.User.Password() if set && pwd != "" { c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd))) r := bufio.NewReader(c) line, err := r.ReadString('\n') if err != nil { return err } if line[0] != '+' { return fmt.Errorf("%s", strings.TrimSpace(line)[1:]) } } } g.c = c } g.c.Write([]byte("info\r\n")) r := bufio.NewReader(g.c) line, err := r.ReadString('\n') if err != nil { return err } if line[0] != '$' { return fmt.Errorf("bad line start: %s", ErrProtocolError) } line = strings.TrimSpace(line) szStr := line[1:] sz, err := strconv.Atoi(szStr) if err != nil { return fmt.Errorf("bad size string <<%s>>: %s", szStr, ErrProtocolError) } var read int for read < sz { line, err := r.ReadString('\n') if err != nil { return err } read += len(line) if len(line) == 1 || line[0] == '#' { continue } parts := strings.SplitN(line, ":", 2) name := string(parts[0]) metric, ok := Tracking[name] if !ok { continue } tags := map[string]string{"host": addr.String()} val := strings.TrimSpace(parts[1]) ival, err := strconv.ParseUint(val, 10, 64) if err == nil { acc.Add(metric, ival, tags) continue } fval, err := strconv.ParseFloat(val, 64) if err != nil { return err } acc.Add(metric, fval, tags) } return nil }
func (s *DockerStats) Gather(acc plugins.Accumulator) error { containers, err := s.ps.DockerStat() if err != nil { return fmt.Errorf("error getting docker info: %s", err) } for _, cont := range containers { tags := map[string]string{ "id": cont.Id, "name": cont.Name, "command": cont.Command, } cts := cont.CPU acc.Add("user", cts.User, tags) acc.Add("system", cts.System, tags) acc.Add("idle", cts.Idle, tags) acc.Add("nice", cts.Nice, tags) acc.Add("iowait", cts.Iowait, tags) acc.Add("irq", cts.Irq, tags) acc.Add("softirq", cts.Softirq, tags) acc.Add("steal", cts.Steal, tags) acc.Add("guest", cts.Guest, tags) acc.Add("guestNice", cts.GuestNice, tags) acc.Add("stolen", cts.Stolen, tags) acc.Add("cache", cont.Mem.Cache, tags) acc.Add("rss", cont.Mem.RSS, tags) acc.Add("rss_huge", cont.Mem.RSSHuge, tags) acc.Add("mapped_file", cont.Mem.MappedFile, tags) acc.Add("swap_in", cont.Mem.Pgpgin, tags) acc.Add("swap_out", cont.Mem.Pgpgout, tags) acc.Add("page_fault", cont.Mem.Pgfault, tags) acc.Add("page_major_fault", cont.Mem.Pgmajfault, tags) acc.Add("inactive_anon", cont.Mem.InactiveAnon, tags) acc.Add("active_anon", cont.Mem.ActiveAnon, tags) acc.Add("inactive_file", cont.Mem.InactiveFile, tags) acc.Add("active_file", cont.Mem.ActiveFile, tags) acc.Add("unevictable", cont.Mem.Unevictable, tags) acc.Add("memory_limit", cont.Mem.HierarchicalMemoryLimit, tags) acc.Add("total_cache", cont.Mem.TotalCache, tags) acc.Add("total_rss", cont.Mem.TotalRSS, tags) acc.Add("total_rss_huge", cont.Mem.TotalRSSHuge, tags) acc.Add("total_mapped_file", cont.Mem.TotalMappedFile, tags) acc.Add("total_swap_in", cont.Mem.TotalPgpgIn, tags) acc.Add("total_swap_out", cont.Mem.TotalPgpgOut, tags) acc.Add("total_page_fault", cont.Mem.TotalPgFault, tags) acc.Add("total_page_major_fault", cont.Mem.TotalPgMajFault, tags) acc.Add("total_inactive_anon", cont.Mem.TotalInactiveAnon, tags) acc.Add("total_active_anon", cont.Mem.TotalActiveAnon, tags) acc.Add("total_inactive_file", cont.Mem.TotalInactiveFile, tags) acc.Add("total_active_file", cont.Mem.TotalActiveFile, tags) acc.Add("total_unevictable", cont.Mem.TotalUnevictable, tags) } return nil }
func (n *Nginx) gatherUrl(addr *url.URL, acc plugins.Accumulator) error { resp, err := client.Get(addr.String()) if err != nil { return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status) } r := bufio.NewReader(resp.Body) // Active connections _, err = r.ReadString(':') if err != nil { return err } line, err := r.ReadString('\n') if err != nil { return err } active, err := strconv.ParseUint(strings.TrimSpace(line), 10, 64) if err != nil { return err } // Server accepts handled requests _, err = r.ReadString('\n') if err != nil { return err } line, err = r.ReadString('\n') if err != nil { return err } data := strings.SplitN(strings.TrimSpace(line), " ", 3) accepts, err := strconv.ParseUint(data[0], 10, 64) if err != nil { return err } handled, err := strconv.ParseUint(data[1], 10, 64) if err != nil { return err } requests, err := strconv.ParseUint(data[2], 10, 64) if err != nil { return err } // Reading/Writing/Waiting line, err = r.ReadString('\n') if err != nil { return err } data = strings.SplitN(strings.TrimSpace(line), " ", 6) reading, err := strconv.ParseUint(data[1], 10, 64) if err != nil { return err } writing, err := strconv.ParseUint(data[3], 10, 64) if err != nil { return err } waiting, err := strconv.ParseUint(data[5], 10, 64) if err != nil { return err } tags := getTags(addr) acc.Add("active", active, tags) acc.Add("accepts", accepts, tags) acc.Add("handled", handled, tags) acc.Add("requests", requests, tags) acc.Add("reading", reading, tags) acc.Add("writing", writing, tags) acc.Add("waiting", waiting, tags) return nil }
func (s *Storage) AddStats(acc plugins.Accumulator, tags map[string]string) { acc.Add("cache_bytes_in_use", s.Cache.BytesInUse, tags) acc.Add("disk_read_bytes_per_sec", s.Disk.ReadBytesPerSec, tags) acc.Add("disk_read_bytes_total", s.Disk.ReadBytesTotal, tags) acc.Add("disk_written_bytes_per_sec", s.Disk.WriteBytesPerSec, tags) acc.Add("disk_written_bytes_total", s.Disk.WriteBytesTotal, tags) acc.Add("disk_usage_data_bytes", s.Disk.SpaceUsage.Data, tags) acc.Add("disk_usage_garbage_bytes", s.Disk.SpaceUsage.Garbage, tags) acc.Add("disk_usage_metadata_bytes", s.Disk.SpaceUsage.Metadata, tags) acc.Add("disk_usage_preallocated_bytes", s.Disk.SpaceUsage.Prealloc, tags) }
func (p *Postgresql) accRow(row scanner, acc plugins.Accumulator, server string) error { var ignore interface{} var name string var commit, rollback, read, hit int64 var returned, fetched, inserted, updated, deleted int64 var conflicts, temp_files, temp_bytes, deadlocks int64 var read_time, write_time float64 err := row.Scan(&ignore, &name, &ignore, &commit, &rollback, &read, &hit, &returned, &fetched, &inserted, &updated, &deleted, &conflicts, &temp_files, &temp_bytes, &deadlocks, &read_time, &write_time, &ignore, ) if err != nil { return err } tags := map[string]string{"server": server, "db": name} acc.Add("xact_commit", commit, tags) acc.Add("xact_rollback", rollback, tags) acc.Add("blks_read", read, tags) acc.Add("blks_hit", hit, tags) acc.Add("tup_returned", returned, tags) acc.Add("tup_fetched", fetched, tags) acc.Add("tup_inserted", inserted, tags) acc.Add("tup_updated", updated, tags) acc.Add("tup_deleted", deleted, tags) acc.Add("conflicts", conflicts, tags) acc.Add("temp_files", temp_files, tags) acc.Add("temp_bytes", temp_bytes, tags) acc.Add("deadlocks", deadlocks, tags) acc.Add("blk_read_time", read_time, tags) acc.Add("blk_write_time", read_time, tags) return nil }
func (n *Apache) gatherScores(data string, acc plugins.Accumulator, tags map[string]string) { var waiting, open int = 0, 0 var S, R, W, K, D, C, L, G, I int = 0, 0, 0, 0, 0, 0, 0, 0, 0 for _, s := range strings.Split(data, "") { switch s { case "_": waiting++ case "S": S++ case "R": R++ case "W": W++ case "K": K++ case "D": D++ case "C": C++ case "L": L++ case "G": G++ case "I": I++ case ".": open++ } } acc.Add("scboard_waiting", float64(waiting), tags) acc.Add("scboard_starting", float64(S), tags) acc.Add("scboard_reading", float64(R), tags) acc.Add("scboard_sending", float64(W), tags) acc.Add("scboard_keepalive", float64(K), tags) acc.Add("scboard_dnslookup", float64(D), tags) acc.Add("scboard_closing", float64(C), tags) acc.Add("scboard_logging", float64(L), tags) acc.Add("scboard_finishing", float64(G), tags) acc.Add("scboard_idle_cleanup", float64(I), tags) acc.Add("scboard_open", float64(open), tags) }
func (s *DiskIOStats) Gather(acc plugins.Accumulator) error { diskio, err := s.ps.DiskIO() if err != nil { return fmt.Errorf("error getting disk io info: %s", err) } for _, io := range diskio { tags := map[string]string{} if len(io.Name) != 0 { tags["name"] = io.Name } if len(io.SerialNumber) != 0 { tags["serial"] = io.SerialNumber } acc.Add("reads", io.ReadCount, tags) acc.Add("writes", io.WriteCount, tags) acc.Add("read_bytes", io.ReadBytes, tags) acc.Add("write_bytes", io.WriteBytes, tags) acc.Add("read_time", io.ReadTime, tags) acc.Add("write_time", io.WriteTime, tags) acc.Add("io_time", io.IoTime, tags) } return nil }
func (s *DiskStats) Gather(acc plugins.Accumulator) error { disks, err := s.ps.DiskUsage() if err != nil { return fmt.Errorf("error getting disk usage info: %s", err) } for _, du := range disks { tags := map[string]string{ "path": du.Path, "fstype": du.Fstype, } acc.Add("total", du.Total, tags) acc.Add("free", du.Free, tags) acc.Add("used", du.Total-du.Free, tags) acc.Add("inodes_total", du.InodesTotal, tags) acc.Add("inodes_free", du.InodesFree, tags) acc.Add("inodes_used", du.InodesTotal-du.InodesFree, tags) } return nil }
func (r *RabbitMQ) gatherServer(serv *Server, acc plugins.Accumulator) error { overview := &OverviewResponse{} err := r.requestJSON(serv, "/api/overview", &overview) if err != nil { return err } tags := map[string]string{} acc.Add("messages", overview.QueueTotals.Messages, tags) acc.Add("messages_ready", overview.QueueTotals.MessagesReady, tags) acc.Add("messages_unacked", overview.QueueTotals.MessagesUnacknowledged, tags) acc.Add("channels", overview.ObjectTotals.Channels, tags) acc.Add("connections", overview.ObjectTotals.Connections, tags) acc.Add("consumers", overview.ObjectTotals.Consumers, tags) acc.Add("exchanges", overview.ObjectTotals.Exchanges, tags) acc.Add("queues", overview.ObjectTotals.Queues, tags) if overview.MessageStats != nil { acc.Add("messages_acked", overview.MessageStats.Ack, tags) acc.Add("messages_delivered", overview.MessageStats.Deliver, tags) acc.Add("messages_published", overview.MessageStats.Publish, tags) } nodes := make([]Node, 0) err = r.requestJSON(serv, "/api/nodes", &nodes) if err != nil { return err } for _, node := range nodes { if !shouldGatherNode(node, serv) { continue } tags = map[string]string{"node": node.Name} acc.Add("disk_free", node.DiskFree, tags) acc.Add("disk_free_limit", node.DiskFreeLimit, tags) acc.Add("fd_total", node.FdTotal, tags) acc.Add("fd_used", node.FdUsed, tags) acc.Add("mem_limit", node.MemLimit, tags) acc.Add("mem_used", node.MemUsed, tags) acc.Add("proc_total", node.ProcTotal, tags) acc.Add("proc_used", node.ProcUsed, tags) acc.Add("run_queue", node.RunQueue, tags) acc.Add("sockets_total", node.SocketsTotal, tags) acc.Add("sockets_used", node.SocketsUsed, tags) } return nil }
func (m *Memcached) gatherServer(address string, acc plugins.Accumulator) error { _, _, err := net.SplitHostPort(address) if err != nil { address = address + ":11211" } // Connect conn, err := net.DialTimeout("tcp", address, defaultTimeout) if err != nil { return err } defer conn.Close() // Extend connection conn.SetDeadline(time.Now().Add(defaultTimeout)) // Read and write buffer rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)) // Send command if _, err = fmt.Fprint(rw, "stats\r\n"); err != nil { return err } if err = rw.Flush(); err != nil { return err } // Read response values := make(map[string]string) for { // Read line line, _, errRead := rw.Reader.ReadLine() if errRead != nil { return errRead } // Done if bytes.Equal(line, []byte("END")) { break } // Read values s := bytes.SplitN(line, []byte(" "), 3) if len(s) != 3 || !bytes.Equal(s[0], []byte("STAT")) { return fmt.Errorf("unexpected line in stats response: %q", line) } // Save values values[string(s[1])] = string(s[2]) } // Add server address as a tag tags := map[string]string{"server": address} // Process values for _, key := range sendAsIs { if value, ok := values[key]; ok { // Mostly it is the number if iValue, errParse := strconv.ParseInt(value, 10, 64); errParse != nil { acc.Add(key, value, tags) } else { acc.Add(key, iValue, tags) } } } return nil }
func (m *Mysql) gatherServer(serv string, acc plugins.Accumulator) error { if serv == "localhost" { serv = "" } db, err := sql.Open("mysql", serv) if err != nil { return err } defer db.Close() rows, err := db.Query(`SHOW /*!50002 GLOBAL */ STATUS`) if err != nil { return err } for rows.Next() { var name string var val interface{} err = rows.Scan(&name, &val) if err != nil { return err } var found bool // Parse out user/password from server address tag if given var servtag string if strings.Contains(serv, "@") { servtag = strings.Split(serv, "@")[1] } else { servtag = serv } tags := map[string]string{"server": servtag} for _, mapped := range mappings { if strings.HasPrefix(name, mapped.onServer) { i, _ := strconv.Atoi(string(val.([]byte))) acc.Add(mapped.inExport+name[len(mapped.onServer):], i, tags) found = true } } if found { continue } switch name { case "Queries": i, err := strconv.ParseInt(string(val.([]byte)), 10, 64) if err != nil { return err } acc.Add("queries", i, tags) case "Slow_queries": i, err := strconv.ParseInt(string(val.([]byte)), 10, 64) if err != nil { return err } acc.Add("slow_queries", i, tags) } } return nil }
func (_ *SystemStats) add(acc plugins.Accumulator, name string, val float64, tags map[string]string) { if val >= 0 { acc.Add(name, val, tags) } }