Example #1
0
// Read RuntimeConfig from io.Reader. will append hash of read data.
func ReadRuntimeConfig(r io.Reader) (*RuntimeConfig, error) {
	var rconf RuntimeConfig
	var err error

	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	// we have to use viper to load config. coz yaml cannot unmarshal
	// complicated struct. inside of viper. it use mapstructure to do it.
	nr := bytes.NewReader(data)
	vp := viper.New()
	vp.SetConfigType("yaml")
	err = vp.ReadConfig(nr)
	if err != nil {
		return nil, err
	}
	err = vp.Marshal(&rconf)
	if err != nil {
		return nil, err
	}

	h := md5.New()
	h.Write(data)
	rconf.hash = hex.EncodeToString(h.Sum(nil))
	rconf.rawdata = data

	return &rconf, nil
}
Example #2
0
func TestViperParseMetric(t *testing.T) {
	type conf struct {
		Metric Metric `json:"metric"`
	}

	var c conf

	c_str := []byte(`{"metric": "win.wmi.fs.d.cdfs.free_space.bytes ahahah"}`)

	vp := viper.New()
	vp.SetConfigType("json")
	vp.ReadConfig(bytes.NewBuffer(c_str))
	vp.Marshal(&c)
	res := c.Metric.Clean()
	t.Log(res)
	// if res != "win.wmi.fs.d.cdfs.free_space.bytesahahah" { // ReplaceVersion
	if res != "win.wmi.fs.d.cdfs.free_space.bytes_ahahah" { // Regex Version

		t.Error("failed")
	}
}
Example #3
0
func _must_win_sys_wmi(name, prefix, interval string) newcore.Collector {
	wmi_config_str_tpl := `
interval: {-----}
queries:
    -
        query: "select Name, NumberOfCores from Win32_Processor"
        metrics:
            -
                value_from: "Name"
                metric: "win.wmi.cpu.name"
            -
                value_from: "NumberOfCores"
                metric: "win.wmi.cpu.numberofcores"
    -
        query: "select Name, Size from Win32_LogicalDisk where MediaType=11 or mediatype=12"
        metrics:
            -
                value_from: "Size"
                metric: "win.wmi.logicaldisk.total_size_bytes"
                tags: {
                    "mount": "{{.Name}}",
                }
`
	wmi_config_str := strings.Replace(wmi_config_str_tpl, "{-----}", interval, 1)

	logging.Tracef("pdh_config_str: %s", wmi_config_str)

	wmi_viper := viper.New()
	wmi_viper.SetConfigType("yaml")
	wmi_viper.ReadConfig(bytes.NewBuffer([]byte(wmi_config_str)))

	var tmp_wmi_conf c_conf.Config_win_wmi
	wmi_viper.Marshal(&tmp_wmi_conf)

	return MustNewWinWmiCollector(name, prefix, tmp_wmi_conf)
}
Example #4
0
func _must_win_sys_pdh(name, prefix, interval string) newcore.Collector {
	pdh_config_str_tpl := `
interval: {-----}
queries:
    -
        # CPU load
        query: "\\System\\Processor Queue Length"
        metric: "win.pdh.processor_queue_length"
    -
        query: "\\Processor(_Total)\\% Processor Time"
        metric: "win.pdh.pct_processor_time"
    -
        query: "\\Memory\\Available KBytes"
        metric: "win.pdh.memory.available_kbytes"
    -
        query: "\\Memory\\% Committed Bytes In Use"
        metric: "win.pdh.memory.pct_committed_bytes_in_use"
    -
        query: "\\PhysicalDisk(_Total)\\Avg. Disk sec/Read"
        metric: "win.pdh.physical_disk.avg_disk_sec_read"
    -
        query: "\\PhysicalDisk(_Total)\\Avg. Disk sec/Write"
        metric: "win.pdh.physical_disk.avg_disk_sec_write"
    -
        query: "\\TCPv4\\Connections Established"
        metric: "win.pdh.tcpv4.connections_established"
    -
        query: "\\System\\System Calls/sec"
        metric: "win.pdh.system.system_calls_sec"
    -
        query: "\\PhysicalDisk(_Total)\\Avg. Disk Bytes/Transfer"
        metric: "win.pdh.physical_disk.avg_disk_bytes_transfer"
    -
        query: "\\PhysicalDisk(_Total)\\Avg. Disk Queue Length"
        metric: "win.pdh.physical_disk.avg_disk_queue_length"
    -
        query: "\\PhysicalDisk(_Total)\\% Disk Time"
        metric: "win.pdh.physical_disk.pct_disk_time"
    -
        query: "\\LogicalDisk(C:)\\% Free Space"
        metric: "win.pdh.logicaldisk.pct_free_space_c"
    -
        query: "\\LogicalDisk(C:)\\Free Megabytes"
        metric: "win.pdh.logicaldisk.free_mbytes_c"
    -
        query: "\\LogicalDisk(D:)\\% Free Space"
        metric: "win.pdh.logicaldisk.pct_free_space_d"
    -
        query: "\\LogicalDisk(D:)\\Free Megabytes"
        metric: "win.pdh.logicaldisk.free_mbytes_d"
    -
        query: "\\TCPv4\\Connections Reset"
        metric: "win.pdh.tcpv4.connections_reset"
    -
        query: "\\TCPv4\\Connection Failures"
        metric: "win.pdh.tcpv4.connections_failures"

`
	pdh_config_str := strings.Replace(pdh_config_str_tpl, "{-----}", interval, 1)

	logging.Tracef("pdh_config_str: %s", pdh_config_str)

	pdh_viper := viper.New()
	pdh_viper.SetConfigType("yaml")
	pdh_viper.ReadConfig(bytes.NewBuffer([]byte(pdh_config_str)))
	var tmp_pdh_conf c_conf.Config_win_pdh_collector
	pdh_viper.Marshal(&tmp_pdh_conf)

	return MustNewWinPdhCollector(name, prefix, tmp_pdh_conf)
}
Example #5
0
func TestWinWmiCollectorViper1(t *testing.T) {

	opts_str := []byte(`
interval: 1s
queries: 
    - 
        query: "select Name, FileSystem, FreeSpace, Size from Win32_LogicalDisk where MediaType=11 or mediatype=12"
        metrics:
            -
                value_from: "Size"
                metric: "win.wmi.fs.size.bytes"
                tags: {
                    "mount": "{{.Name}}",
                }
            -
                value_from: "FreeSpace"
                metric: "win.wmi.fs.freespace.bytes"
                tags: {
                    "mount": "{{.Name}}",
                }
`)
	vp := viper.New()
	var opts config.Config_win_wmi

	vp.SetConfigType("yaml")
	vp.ReadConfig(bytes.NewBuffer(opts_str))
	vp.Marshal(&opts)

	fmt.Printf("opts loaded from viper: %+v \n", opts)

	sub := newcore.Subscribe(MustNewWinWmiCollector("c1", "prefix", opts), nil)

	time.AfterFunc(time.Second*1, func() {
		sub.Close()
	})

	timeout := time.After(time.Second * time.Duration(2))

main_loop:
	for {
		select {
		case md, openning := <-sub.Updates():
			if openning {
				if md == nil {
					fmt.Println("md is nil")
				} else {
					is_mount_c_exists := false

					for _, dp := range md {
						fmt.Println("dp: ---> ", dp)
						if _, ok := dp.Tags["host"]; ok == false {
							t.Error("host is not in tags")
							return
						}
						if !strings.HasPrefix(dp.Metric.Clean(), "prefix.win.wmi.") {
							t.Error("metric wrong")
							return
						}
						m, ok := dp.Tags["mount"]
						if ok && strings.ToLower(m) == "c" {
							is_mount_c_exists = true
						}
					}

					if is_mount_c_exists == false {
						t.Error("mount c is not exists")
					}
				}
			} else {
				break main_loop
			}
		case <-timeout:
			t.Error("timed out! something is blocking")
			break main_loop
		}
	}
}