mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-10-30 00:25:06 +01:00 
			
		
		
		
	Format fixes
This commit is contained in:
		| @@ -1,31 +1,31 @@ | |||||||
| package collectors | package collectors | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"encoding/json" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	lp "github.com/influxdata/line-protocol" | 	lp "github.com/influxdata/line-protocol" | ||||||
| 	"log" | 	"log" | ||||||
|  | 	"os" | ||||||
|  | 	"os/exec" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 	"os" |  | ||||||
| 	"os/exec" |  | ||||||
| 	"encoding/json" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const IPMITOOL_PATH = `/usr/bin/ipmitool` | const IPMITOOL_PATH = `/usr/bin/ipmitool` | ||||||
| const IPMISENSORS_PATH = `/usr/sbin/ipmi-sensors` | const IPMISENSORS_PATH = `/usr/sbin/ipmi-sensors` | ||||||
|  |  | ||||||
| type IpmiCollectorConfig struct { | type IpmiCollectorConfig struct { | ||||||
|     ExcludeDevices []string `json:"exclude_devices"` | 	ExcludeDevices  []string `json:"exclude_devices"` | ||||||
|     IpmitoolPath string `json:"ipmitool_path"` | 	IpmitoolPath    string   `json:"ipmitool_path"` | ||||||
|     IpmisensorsPath string `json:"ipmisensors_path"` | 	IpmisensorsPath string   `json:"ipmisensors_path"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type IpmiCollector struct { | type IpmiCollector struct { | ||||||
| 	MetricCollector | 	MetricCollector | ||||||
| 	tags    map[string]string | 	tags    map[string]string | ||||||
| 	matches map[string]string | 	matches map[string]string | ||||||
| 	config IpmiCollectorConfig | 	config  IpmiCollectorConfig | ||||||
| } | } | ||||||
|  |  | ||||||
| func (m *IpmiCollector) Init(config []byte) error { | func (m *IpmiCollector) Init(config []byte) error { | ||||||
| @@ -40,13 +40,13 @@ func (m *IpmiCollector) Init(config []byte) error { | |||||||
| 	_, err1 := os.Stat(m.config.IpmitoolPath) | 	_, err1 := os.Stat(m.config.IpmitoolPath) | ||||||
| 	_, err2 := os.Stat(m.config.IpmisensorsPath) | 	_, err2 := os.Stat(m.config.IpmisensorsPath) | ||||||
| 	if err1 != nil { | 	if err1 != nil { | ||||||
| 	    m.config.IpmitoolPath = "" | 		m.config.IpmitoolPath = "" | ||||||
| 	} | 	} | ||||||
| 	if err2 != nil { | 	if err2 != nil { | ||||||
| 	    m.config.IpmisensorsPath = "" | 		m.config.IpmisensorsPath = "" | ||||||
| 	} | 	} | ||||||
| 	if err1 != nil && err2 != nil { | 	if err1 != nil && err2 != nil { | ||||||
| 	    return errors.New("No IPMI reader found") | 		return errors.New("No IPMI reader found") | ||||||
| 	} | 	} | ||||||
| 	m.init = true | 	m.init = true | ||||||
| 	return nil | 	return nil | ||||||
| @@ -66,23 +66,23 @@ func ReadIpmiTool(cmd string, out *[]lp.MutableMetric) { | |||||||
| 	for _, line := range ll { | 	for _, line := range ll { | ||||||
| 		lv := strings.Split(line, "|") | 		lv := strings.Split(line, "|") | ||||||
| 		if len(lv) < 3 { | 		if len(lv) < 3 { | ||||||
|             continue | 			continue | ||||||
|         } | 		} | ||||||
| 		v, err := strconv.ParseFloat(strings.Trim(lv[1], " "), 64) | 		v, err := strconv.ParseFloat(strings.Trim(lv[1], " "), 64) | ||||||
| 		if err == nil { | 		if err == nil { | ||||||
| 			name := strings.ToLower(strings.Replace(strings.Trim(lv[0], " "), " ", "_", -1)) | 			name := strings.ToLower(strings.Replace(strings.Trim(lv[0], " "), " ", "_", -1)) | ||||||
| 			unit := strings.Trim(lv[2], " ") | 			unit := strings.Trim(lv[2], " ") | ||||||
| 			if unit == "Volts" { | 			if unit == "Volts" { | ||||||
| 			    unit = "V" | 				unit = "V" | ||||||
| 			} else if unit == "degrees C" { | 			} else if unit == "degrees C" { | ||||||
| 			    unit = "C" | 				unit = "C" | ||||||
| 			} else if unit == "degrees F" { | 			} else if unit == "degrees F" { | ||||||
| 			    unit = "F" | 				unit = "F" | ||||||
| 			} else if unit == "Watts" { | 			} else if unit == "Watts" { | ||||||
| 			    unit = "W" | 				unit = "W" | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			y, err := lp.New(name, map[string]string{"unit": unit, "type" : "node"}, map[string]interface{}{"value": v}, time.Now()) | 			y, err := lp.New(name, map[string]string{"unit": unit, "type": "node"}, map[string]interface{}{"value": v}, time.Now()) | ||||||
| 			if err == nil { | 			if err == nil { | ||||||
| 				*out = append(*out, y) | 				*out = append(*out, y) | ||||||
| 			} | 			} | ||||||
| @@ -105,26 +105,26 @@ func ReadIpmiSensors(cmd string, out *[]lp.MutableMetric) { | |||||||
| 	for _, line := range ll { | 	for _, line := range ll { | ||||||
| 		lv := strings.Split(line, ",") | 		lv := strings.Split(line, ",") | ||||||
| 		if len(lv) > 3 { | 		if len(lv) > 3 { | ||||||
| 		    v, err := strconv.ParseFloat(lv[3], 64) | 			v, err := strconv.ParseFloat(lv[3], 64) | ||||||
| 		    if err == nil { | 			if err == nil { | ||||||
| 			    name := strings.ToLower(strings.Replace(lv[1], " ", "_", -1)) | 				name := strings.ToLower(strings.Replace(lv[1], " ", "_", -1)) | ||||||
| 			    y, err := lp.New(name, map[string]string{"unit": lv[4], "type" : "node"}, map[string]interface{}{"value": v}, time.Now()) | 				y, err := lp.New(name, map[string]string{"unit": lv[4], "type": "node"}, map[string]interface{}{"value": v}, time.Now()) | ||||||
| 			    if err == nil { | 				if err == nil { | ||||||
| 				    *out = append(*out, y) | 					*out = append(*out, y) | ||||||
| 			    } | 				} | ||||||
| 		    } | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (m *IpmiCollector) Read(interval time.Duration, out *[]lp.MutableMetric) { | func (m *IpmiCollector) Read(interval time.Duration, out *[]lp.MutableMetric) { | ||||||
|     if len(m.config.IpmitoolPath) > 0 { | 	if len(m.config.IpmitoolPath) > 0 { | ||||||
| 	    _, err := os.Stat(m.config.IpmitoolPath) | 		_, err := os.Stat(m.config.IpmitoolPath) | ||||||
| 	    if err == nil { | 		if err == nil { | ||||||
| 		    ReadIpmiTool(m.config.IpmitoolPath, out) | 			ReadIpmiTool(m.config.IpmitoolPath, out) | ||||||
| 		} | 		} | ||||||
| 	} else if len(m.config.IpmisensorsPath) > 0 { | 	} else if len(m.config.IpmisensorsPath) > 0 { | ||||||
| 	    _, err := os.Stat(m.config.IpmisensorsPath) | 		_, err := os.Stat(m.config.IpmisensorsPath) | ||||||
| 		if err == nil { | 		if err == nil { | ||||||
| 			ReadIpmiSensors(m.config.IpmisensorsPath, out) | 			ReadIpmiSensors(m.config.IpmisensorsPath, out) | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -32,11 +32,11 @@ func (m *NetstatCollector) Init(config []byte) error { | |||||||
| 		10: "pkts_out", | 		10: "pkts_out", | ||||||
| 	} | 	} | ||||||
| 	if len(config) > 0 { | 	if len(config) > 0 { | ||||||
| 	    err := json.Unmarshal(config, &m.config) | 		err := json.Unmarshal(config, &m.config) | ||||||
| 	    if err != nil { | 		if err != nil { | ||||||
| 		    log.Print(err.Error()) | 			log.Print(err.Error()) | ||||||
| 		    return err | 			return err | ||||||
| 	    } | 		} | ||||||
| 	} | 	} | ||||||
| 	_, err := ioutil.ReadFile(string(NETSTATFILE)) | 	_, err := ioutil.ReadFile(string(NETSTATFILE)) | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| package collectors | package collectors | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	lp "github.com/influxdata/line-protocol" | 	lp "github.com/influxdata/line-protocol" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| @@ -9,15 +10,13 @@ import ( | |||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 	"encoding/json" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const HWMON_PATH = `/sys/class/hwmon` | const HWMON_PATH = `/sys/class/hwmon` | ||||||
|  |  | ||||||
|  |  | ||||||
| type TempCollectorConfig struct { | type TempCollectorConfig struct { | ||||||
| 	ExcludeMetrics []string `json:"exclude_metrics"` | 	ExcludeMetrics []string                     `json:"exclude_metrics"` | ||||||
| 	TagOverride map[string]map[string]string `json:"tag_override"` | 	TagOverride    map[string]map[string]string `json:"tag_override"` | ||||||
| } | } | ||||||
|  |  | ||||||
| type TempCollector struct { | type TempCollector struct { | ||||||
|   | |||||||
| @@ -34,7 +34,7 @@ func (m *TopProcsCollector) Init(config []byte) error { | |||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 	    m.config.num_procs = int(DEFAULT_NUM_PROCS) | 		m.config.num_procs = int(DEFAULT_NUM_PROCS) | ||||||
| 	} | 	} | ||||||
| 	if m.config.num_procs <= 0 || m.config.num_procs > MAX_NUM_PROCS { | 	if m.config.num_procs <= 0 || m.config.num_procs > MAX_NUM_PROCS { | ||||||
| 		return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS)) | 		return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS)) | ||||||
|   | |||||||
| @@ -31,7 +31,7 @@ var Collectors = map[string]collectors.MetricGetter{ | |||||||
| 	"customcmd":  &collectors.CustomCmdCollector{}, | 	"customcmd":  &collectors.CustomCmdCollector{}, | ||||||
| 	"diskstat":   &collectors.DiskstatCollector{}, | 	"diskstat":   &collectors.DiskstatCollector{}, | ||||||
| 	"tempstat":   &collectors.TempCollector{}, | 	"tempstat":   &collectors.TempCollector{}, | ||||||
| 	"ipmistat" :  &collectors.IpmiCollector{}, | 	"ipmistat":   &collectors.IpmiCollector{}, | ||||||
| } | } | ||||||
|  |  | ||||||
| var Sinks = map[string]sinks.SinkFuncs{ | var Sinks = map[string]sinks.SinkFuncs{ | ||||||
| @@ -244,10 +244,10 @@ func main() { | |||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Print("SKIP ", col.Name(), " (", err.Error(), ")") | 			log.Print("SKIP ", col.Name(), " (", err.Error(), ")") | ||||||
| 		} else if !col.Initialized() { | 		} else if !col.Initialized() { | ||||||
| 		    log.Print("SKIP ", col.Name(), " (Not initialized)") | 			log.Print("SKIP ", col.Name(), " (Not initialized)") | ||||||
| 	    } else { | 		} else { | ||||||
| 		    log.Print("Start ", col.Name()) | 			log.Print("Start ", col.Name()) | ||||||
| 		    tmp = append(tmp, c) | 			tmp = append(tmp, c) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	config.Collectors = tmp | 	config.Collectors = tmp | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user