Esempio n. 1
0
func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
	loadavg, err := load.LoadAvg()
	if err != nil {
		return err
	}

	hostinfo, err := host.HostInfo()
	if err != nil {
		return err
	}

	users, err := host.Users()
	if err != nil {
		return err
	}

	fields := map[string]interface{}{
		"load1":         loadavg.Load1,
		"load5":         loadavg.Load5,
		"load15":        loadavg.Load15,
		"uptime":        hostinfo.Uptime,
		"n_users":       len(users),
		"uptime_format": format_uptime(hostinfo.Uptime),
		"n_cpus":        runtime.NumCPU(),
	}
	acc.AddFields("system", fields, nil)

	return nil
}
func loadAvg(ns []string) (*plugin.PluginMetricType, error) {
	load, err := load.LoadAvg()
	if err != nil {
		return nil, err
	}

	switch joinNamespace(ns) {
	case "/intel/psutil/load/load1":
		return &plugin.PluginMetricType{
			Namespace_: ns,
			Data_:      load.Load1,
		}, nil
	case "/intel/psutil/load/load5":
		return &plugin.PluginMetricType{
			Namespace_: ns,
			Data_:      load.Load5,
		}, nil
	case "/intel/psutil/load/load15":
		return &plugin.PluginMetricType{
			Namespace_: ns,
			Data_:      load.Load15,
		}, nil
	}

	return nil, fmt.Errorf("Unknown error processing %v", ns)
}
Esempio n. 3
0
func loadAvg() string {
	l, err := load.LoadAvg()
	if err != nil {
		log.Println(err)
	}
	return fmt.Sprintf("%5s %5s %5s", humanize.Ftoa(l.Load1), humanize.Ftoa(l.Load5), humanize.Ftoa(l.Load15))
}
Esempio n. 4
0
func startNode(masterHost, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	masterUrl := url.URL{Scheme: "tcp", Host: masterHost}
	ip := getPrivateIP()

	// Try to get new "respondent" socket
	if sock, err = respondent.NewSocket(); err != nil {
		utils.Die("Can't get new respondent socket: %s", err.Error())
	}
	defer sock.Close()

	sock.AddTransport(tcp.NewTransport())

	// Connect to master
	if err = sock.Dial(masterUrl.String()); err != nil {
		utils.Die("Can't dial on respondent socket: %s", err.Error())
	}

	// Wait for a survey request and send responses
	for {
		if msg, err = sock.Recv(); err != nil {
			utils.Die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("Client(%s): Received \"%s\" survey request\n", name, string(msg))

		var loadAvg *load.LoadAvgStat
		if loadAvg, err = load.LoadAvg(); err != nil {
			utils.Die("Cannot get load average: %s", err.Error())
		}

		var cpuInfo []cpu.CPUInfoStat
		if cpuInfo, err = cpu.CPUInfo(); err != nil {
			utils.Die("Cannot get CPU info: %s", err.Error())
		}
		fmt.Printf("CPU INFO len: %d\n", len(cpuInfo))

		// Get the normalized CPU load
		avg := loadAvg.Load1
		cores := int32(0)
		for _, info := range cpuInfo {
			fmt.Printf("Inner Cores: %d\n", info.Cores)
			cores += info.Cores
		}
		fmt.Printf("Load avg: %f\n", avg)
		fmt.Printf("Cores: %d\n", cores)
		avg = avg / float64(cores)

		fmt.Printf("Client(%s): Sending survey response\n", name)
		if err = sock.Send([]byte(fmt.Sprintf("%s,%f", ip, avg))); err != nil {
			utils.Die("Cannot send: %s", err.Error())
		}
	}
}
Esempio n. 5
0
func Load_f() (*LoadAvgStat_j, error) {
	load, _ := load.LoadAvg()
	timestamp := time.Now().Unix()

	//	fmt.Println(mem)
	ret := &LoadAvgStat_j{
		Load1:     load.Load1,
		Load5:     load.Load5,
		Load15:    load.Load15,
		Timestamp: timestamp,
	}
	return ret, nil

}
Esempio n. 6
0
// LoadAverage - returns load avg
func LoadAverage() LoadStruct {

	cores, _ := cpu.CPUCounts(true)
	load, _ := load.LoadAvg()

	l := LoadStruct{
		Minute:         load.Load1,
		FiveMinutes:    load.Load5,
		FifteenMinutes: load.Load15,
		Cores:          cores,
	}

	return l
}
Esempio n. 7
0
func (_ *SystemStats) Gather(acc plugins.Accumulator) error {
	loadavg, err := load.LoadAvg()
	if err != nil {
		return err
	}

	hostinfo, err := host.HostInfo()
	if err != nil {
		return err
	}

	acc.Add("load1", loadavg.Load1, nil)
	acc.Add("load5", loadavg.Load5, nil)
	acc.Add("load15", loadavg.Load15, nil)
	acc.Add("uptime", hostinfo.Uptime, nil)
	acc.Add("uptime_format", format_uptime(hostinfo.Uptime), nil)

	return nil
}
Esempio n. 8
0
func (_ *SystemStats) Gather(acc inputs.Accumulator) error {
	loadavg, err := load.LoadAvg()
	if err != nil {
		return err
	}

	hostinfo, err := host.HostInfo()
	if err != nil {
		return err
	}

	fields := map[string]interface{}{
		"load1":         loadavg.Load1,
		"load5":         loadavg.Load5,
		"load15":        loadavg.Load15,
		"uptime":        hostinfo.Uptime,
		"uptime_format": format_uptime(hostinfo.Uptime),
	}
	acc.AddFields("system", fields, nil)

	return nil
}
Esempio n. 9
0
func getStats() *hash {
	var m, l *hash

	if stat, err := mem.VirtualMemory(); err == nil {
		m = &hash{
			"total":      bytesToKB(stat.Total),
			"free":       bytesToKB(stat.Free),
			"buffers":    bytesToKB(stat.Buffers),
			"cached":     bytesToKB(stat.Cached),
			"free_total": bytesToKB(stat.Free + stat.Buffers + stat.Cached),
		}
	}

	if stat, err := load.LoadAvg(); err == nil {
		l = &hash{
			"one":     stat.Load1,
			"five":    stat.Load5,
			"fifteen": stat.Load15,
		}
	}

	return &hash{"mem": m, "load": l}
}
Esempio n. 10
0
func main() {
	header, _ := json.Marshal(Header{Version: 1})
	fmt.Println(string(header))
	fmt.Println("[")
	go notifyd()
	for {
		if mode == "standard" {
			line := make([]Block, 0)

			// TIME block
			t := time.Now()
			const t_format = "2006-01-02 15:04:05"
			time_block := Block{
				Name:     "time",
				FullText: t.Format(t_format),
				Color:    GRAY,
			}

			// LOAD block
			load, _ := load.LoadAvg()
			load_block := Block{
				Name:     "load",
				FullText: strconv.FormatFloat(load.Load1, 'f', 2, 64),
			}
			if load.Load1 > 2 {
				load_block.Color = BAD
			} else if load.Load1 > 1 {
				load_block.Color = WARN
			} else {
				load_block.Color = OKAY
			}

			// NET block
			net_blocks := make([]Block, 0)
			interfaces, _ := net.NetInterfaces()
			for _, iface := range interfaces {
				if iface.Name == "lo" {
					continue
				}
				text := iface.Addrs[0].Addr
				net_blocks = append(net_blocks, Block{
					Name:     "iface",
					FullText: text,
					Instance: iface.Name,
					Color:    GRAY,
				})
			}

			// HDD block
			root_stat, _ := disk.DiskUsage("/")
			home_stat, _ := disk.DiskUsage("/home")
			data_stat, _ := disk.DiskUsage("/media/me/data")

			root_block := Block{
				Name:     "hdd",
				Instance: "root",
				FullText: FormatGigabytes(root_stat.Free) + "GB",
			}
			if root_stat.Free > 5*1024*1024*1024 {
				root_block.Color = OKAY
			} else if root_stat.Free > 2.5*1024*1024*1024 {
				root_block.Color = WARN
			} else {
				root_block.Color = BAD
			}

			home_block := Block{
				Name:     "hdd",
				Instance: "root",
				FullText: FormatGigabytes(home_stat.Free) + "GB",
			}
			if home_stat.Free > 20*1024*1024*1024 {
				home_block.Color = OKAY
			} else if home_stat.Free > 10*1024*1024*1024 {
				home_block.Color = WARN
			} else {
				home_block.Color = BAD
			}

			data_block := Block{
				Name:     "hdd",
				Instance: "data",
				FullText: FormatGigabytes(data_stat.Free) + "GB",
			}
			if data_stat.Free > 30*1024*1024*1024 {
				data_block.Color = OKAY
			} else if data_stat.Free > 15*1024*1024*1024 {
				data_block.Color = WARN
			} else {
				data_block.Color = BAD
			}

			/* // Headphones block
			headphones := Block {
				Name: "headphones",
				FullText: "Headphones ",
			}
			if exec.Command("sh", "-c", "pacmd list-sinks | grep DR-BTN200").Run() == nil {
				headphones.FullText += "connected";
				headphones.Color = OKAY;
			} else {
				headphones.FullText += "disconnected";
				headphones.Color = BAD;
			} */

			nowplaying := Block{
				Name: "nowplaying",
			}
			nowplaying_text, _ := exec.Command("nowplaying").Output()
			nowplaying.FullText = string(nowplaying_text)

			line = append(line, nowplaying)
			//line = append(line, headphones);
			line = append(line, root_block)
			line = append(line, home_block)
			line = append(line, data_block)
			/*for _, block := range net_blocks {
				line = append(line, block)
			}*/
			line = append(line, load_block)
			line = append(line, time_block)

			PrintLine(line)
			time.Sleep(time.Second * 1)
		} else if mode == "animation" {
			text := anim_text[0:anim_frame]
			anim_frame++
			PrintLine([]Block{Block{Name: "anim", FullText: text, Instance: strconv.Itoa(anim_frame)}})
			if anim_frame > len(anim_text) {
				mode = "standard"
				anim_frame = 0
				time.Sleep(time.Second * 3)
			} else {
				time.Sleep(time.Millisecond * time.Duration(math.Max(35, float64(40-len(anim_text)))))
			}
		}
	}
}
Esempio n. 11
0
func (st *Stat) stat(t string) string {
	checkErr := func(err error) string {
		return "系统酱正在食用作死药丸中..."
	}
	switch t {
	case "free":
		m, err := mem.VirtualMemory()
		checkErr(err)
		s, err := mem.SwapMemory()
		checkErr(err)
		mem := new(runtime.MemStats)
		runtime.ReadMemStats(mem)
		return fmt.Sprintf(
			"全局:\n"+
				"Total: %s Free: %s\nUsed: %s %s%%\nCache: %s\n"+
				"Swap:\nTotal: %s Free: %s\n Used: %s %s%%\n"+
				"群组娘:\n"+
				"Allocated: %s\nTotal Allocated: %s\nSystem: %s\n",
			helper.HumanByte(m.Total, m.Free, m.Used, m.UsedPercent, m.Cached,
				s.Total, s.Free, s.Used, s.UsedPercent,
				mem.Alloc, mem.TotalAlloc, mem.Sys)...,
		)
	case "df":
		fs, err := disk.DiskPartitions(false)
		checkErr(err)
		var buf bytes.Buffer
		for k := range fs {
			du, err := disk.DiskUsage(fs[k].Mountpoint)
			switch {
			case err != nil, du.UsedPercent == 0, du.Free == 0:
				continue
			}
			f := fmt.Sprintf("Mountpoint: %s Type: %s \n"+
				"Total: %s Free: %s \nUsed: %s %s%%\n",
				helper.HumanByte(fs[k].Mountpoint, fs[k].Fstype,
					du.Total, du.Free, du.Used, du.UsedPercent)...,
			)
			buf.WriteString(f)
		}
		return buf.String()
	case "os":
		h, err := host.HostInfo()
		checkErr(err)
		uptime := time.Duration(time.Now().Unix()-int64(h.Uptime)) * time.Second
		l, err := load.LoadAvg()
		checkErr(err)
		c, err := cpu.CPUPercent(time.Second*3, false)
		checkErr(err)
		return fmt.Sprintf(
			"OSRelease: %s\nHostName: %s\nUptime: %s\nLoadAvg: %.2f %.2f %.2f\n"+
				"Goroutine: %d\nCPU: %.2f%%",
			h.Platform, h.Hostname, uptime.String(), l.Load1, l.Load5, l.Load15,
			runtime.NumGoroutine(), c[0],
		)
	case "redis":
		info := conf.Redis.Info().Val()
		if info != "" {
			infos := strings.Split(info, "\r\n")
			infoMap := make(map[string]string)
			for k := range infos {
				line := strings.Split(infos[k], ":")
				if len(line) > 1 {
					infoMap[line[0]] = line[1]
				}
			}
			DBSize := conf.Redis.DbSize().Val()
			return fmt.Sprintf("Redis Version: %s\nOS: %s\nUsed Memory: %s\n"+
				"Used Memory Peak: %s\nDB Size: %d\n",
				infoMap["redis_version"], infoMap["os"], infoMap["used_memory_human"],
				infoMap["used_memory_peak_human"], DBSize)
		}
		return ""
	default:
		return "欢迎来到未知领域(ゝ∀・)"
	}
}
Esempio n. 12
0
func main() {
	httpPort := os.Getenv("PORT")
	if len(httpPort) == 0 {
		httpPort = "8080"
	}
	r := gin.Default()

	// Limit all requests to 10 per second
	//limitHandler := tollbooth_gin.LimitHandler(tollbooth.NewLimiter(600, 60*time.Second))

	limitHandler := func(c *gin.Context) {
		c.Next()
	}

	persistEvents()

	r.Use(staticHandler)
	r.Use(corsHandler)

	r.Use(func(c *gin.Context) {
		start := time.Now()
		c.Next()
		end := time.Now()
		latency := end.Sub(start)

		mutex.Lock()
		if _, ok := db["incr:request"]; !ok {
			db["incr:request"] = incr.NewEvent()
		}
		db["incr:request"].Add(incr.Time(time.Now().Unix()), incr.Value(latency), "")
		mutex.Unlock()
	})

	go func() {
		for _ = range time.Tick(time.Second) {
			mutex.Lock()
			if _, ok := db["incr:goalloc"]; !ok {
				db["incr:goalloc"] = incr.NewEvent()
				db["incr:memused"] = incr.NewEvent()
				db["incr:load:1"] = incr.NewEvent()
				db["incr:load:5"] = incr.NewEvent()
				db["incr:load:15"] = incr.NewEvent()
			}
			now := incr.Time(time.Now().Unix())

			var memstats runtime.MemStats
			runtime.ReadMemStats(&memstats)
			db["incr:goalloc"].Add(now, incr.Value(memstats.Alloc), "")

			if vmem, err := mem.VirtualMemory(); err == nil {
				db["incr:memused"].Add(now, incr.Value(vmem.Used), "")
			}

			if load, err := load.LoadAvg(); err == nil {
				db["incr:load:1"].Add(now, incr.Value(load.Load1), "")
				db["incr:load:5"].Add(now, incr.Value(load.Load5), "")
				db["incr:load:15"].Add(now, incr.Value(load.Load15), "")
			}
			mutex.Unlock()
		}
	}()

	r.Any("/:name/:value", limitHandler, func(c *gin.Context) {
		name := c.Params.ByName("name")
		value, _ := strconv.ParseFloat(c.Params.ByName("value"), 64)
		sender := ""

		if c, err := c.Request.Cookie("sender"); err == nil {
			sender = c.Value
		}

		mutex.Lock()
		defer mutex.Unlock()

		if _, ok := db[name]; !ok {
			// TODO: Limit requests to 10 per day
			db[name] = incr.NewEvent()
		}

		if date := c.Request.Header.Get("Date"); date != "" {
			if d, err := time.Parse(http.TimeFormat, date); err == nil {
				if diff := d.Sub(time.Now()); math.Abs(diff.Hours()) < 24 {
					db[name].Add(incr.Time(d.Unix()), incr.Value(value), sender)
					return
				}
			}
		}
		db[name].Add(incr.Time(time.Now().Unix()), incr.Value(value), sender)
		if c.Request.Method == "GET" {
			c.Data(200, "image/gif", minimalGIF)
		}
	})

	r.GET("/:name", limitHandler, func(c *gin.Context) {
		mutex.RLock()
		defer mutex.RUnlock()
		e, ok := db[c.Params.ByName("name")]
		if !ok {
			e = incr.NewEvent()
		}

		if c.Request.URL.Query().Get("live") != "" {
			c.JSON(200, e.Data(incr.LiveDuration))
		} else if c.Request.URL.Query().Get("hourly") != "" {
			c.JSON(200, e.Data(incr.HourlyDuration))
		} else if c.Request.URL.Query().Get("daily") != "" {
			c.JSON(200, e.Data(incr.DailyDuration))
		} else if c.Request.URL.Query().Get("weekly") != "" {
			c.JSON(200, e.Data(incr.WeeklyDuration))
		} else {
			c.JSON(200, e.Data(0))
		}
	})

	r.Run(":" + httpPort)
}
Esempio n. 13
0
func (o *LoadMetric) Value() (float64, error) {
	l, e := load.LoadAvg()
	return l.Load1, e
}
Esempio n. 14
0
func main() {
	c := config{
		URL:      "http://localhost:8086",
		Database: "test",
		Interval: 5 * time.Minute,
	}

	opts.New(&c).Name("sysflux").Version(VERSION).Parse()

	//validate config
	u, err := url.Parse(c.URL)
	if err != nil {
		log.Fatal("Invalid URL")
	}
	host, port, err := net.SplitHostPort(u.Host)
	if err != nil {
		log.Fatal("Invalid host and port")
	}
	if u.Path == "" {
		u.Path = "/write"
	}
	v := url.Values{}
	v.Set("db", c.Database)
	u.RawQuery = v.Encode()

	tags := ""
	if c.Tags != "" {
		tags = "," + c.Tags
	}

	//good to go
	log.Printf("Using InfluxDB endpoint: %s", u)

	success := false
	lock := sync.Mutex{}
	entries := []string{}

	send := func() error {
		body := strings.NewReader(strings.Join(entries, "\n"))
		if c.DNS != "" {
			//lookup host every POST
			ips, err := lookup(host, c.DNS)
			if err != nil {
				return fmt.Errorf("Lookup failed: %s", err)
			}
			u.Host = ips[0] + ":" + port
		}
		resp, err := http.Post(u.String(), "application/x-www-form-urlencoded", body)
		if err != nil {
			return fmt.Errorf("HTTP POST failed: %s", err)
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusNoContent {
			msg, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				return fmt.Errorf("Response download failed: %s", err)
			}
			return fmt.Errorf("Response: %d => %s", resp.StatusCode, msg)
		}
		//show first success
		if !success {
			log.Printf("Success")
			success = true
		}
		//clear once recieved!
		entries = nil
		return nil
	}

	report := func() {
		t := time.Now().UnixNano()
		if l, err := load.LoadAvg(); err == nil {
			entries = append(entries, fmt.Sprintf("cpu_load_short%s value=%f %d", tags, l.Load1*100, t))
			entries = append(entries, fmt.Sprintf("cpu_load_medium%s value=%f %d", tags, l.Load5*100, t))
			entries = append(entries, fmt.Sprintf("cpu_load_long%s value=%f %d", tags, l.Load15*100, t))
		}
		if v, err := mem.VirtualMemory(); err == nil {
			entries = append(entries, fmt.Sprintf("mem_usage%s value=%f %d", tags, v.UsedPercent, t))
		}
		if len(entries) > MAX_QUEUED {
			entries = entries[len(entries)-MAX_QUEUED:]
		}
	}

	//send loop
	go func() {
		b := backoff.Backoff{}
		for {
			wait := time.Second
			lock.Lock()
			if len(entries) > 0 {
				if err := send(); err == nil {
					b.Reset()
				} else {
					log.Println(err)
					wait = b.Duration()
				}
			}
			lock.Unlock()
			time.Sleep(wait)
		}
	}()

	//report loop
	for {
		lock.Lock()
		report()
		lock.Unlock()
		time.Sleep(c.Interval)
	}
}
Esempio n. 15
0
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("cpus", cpuRsc),
	hateoas.AddLink("load", loadRsc),
	hateoas.AddLink("processes", procRsc),
	hateoas.HEAD(mohttp.EmptyBodyHandler),
)

var cpuRsc = hateoas.NewResource(
	hateoas.Path("/cpus"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return cpu.CPUInfo()
	})),
)

var loadRsc = hateoas.NewResource(
	hateoas.Path("/load"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return load.LoadAvg()
	})),
)

var procRsc = hateoas.NewResource(
	hateoas.Path("/processes"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return process.Pids()
	})),
)
Esempio n. 16
0
func main() {

	flag.Usage = func() {
		fmt.Println(usage)
	}

	flag.Parse()

	for {

		// Docker client remote API
		endpoint := "unix:///var/run/docker.sock"
		dockerClient, _ := docker.NewClient(endpoint)

		// Configuration file settings using key-value
		viper.SetConfigName("plural")
		if *configFlag != "" {
			viper.AddConfigPath(*configFlag)
		} else {
			viper.AddConfigPath("/opt/plural/conf")
		}

		viperReadConfig := viper.ReadInConfig()

		// Default settings if no config file is supplied
		viper.SetDefault("elastic_host", "localhost")
		viper.SetDefault("elastic_port", "9200")
		viper.SetDefault("environment", "dev")
		viper.SetDefault("interval", "300")
		viper.SetDefault("username", "undef")
		viper.SetDefault("password", "undef")
		viper.SetDefault("overwrite", "undef")

		elastic_host := viper.GetString("elastic_host")
		elastic_port := viper.GetString("elastic_port")
		environment := viper.GetString("environment")
		interval := viper.GetInt("interval")
		username := viper.GetString("username")
		password := viper.GetString("password")
		overwrite := viper.GetString("overwrite")

		transport := http.Transport{
			Dial: dialTimeout,
		}

		client := http.Client{
			Transport: &transport,
		}

		cpuCounts, _ := cpu.CPUCounts(true)
		v, _ := mem.VirtualMemory()
		k, _ := disk.DiskUsage("/")
		h, _ := host.HostInfo()
		l, _ := load.LoadAvg()
		memusedprctConv := strconv.FormatFloat(v.UsedPercent, 'f', 6, 64)
		memusedprct := strings.Split(memusedprctConv, ".")[0]
		memfree := humanize.Bytes(v.Free)
		memtotal := humanize.Bytes(v.Total)
		diskusedprctConv := strconv.FormatFloat(k.UsedPercent, 'f', 6, 64)
		diskusedprct := strings.Split(diskusedprctConv, ".")[0]
		diskfree := humanize.Bytes(k.Free)
		disktotal := humanize.Bytes(k.Total)
		timeN := time.Now()
		dateStamp := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", timeN.Year(), timeN.Month(), timeN.Day(), timeN.Hour(), timeN.Minute(), timeN.Second())
		loadoneConv := strconv.FormatFloat(l.Load1, 'f', 6, 64)
		loadone := strings.Split(loadoneConv, ".")[0]
		loadfifteenConv := strconv.FormatFloat(l.Load15, 'f', 6, 64)
		loadfifteen := strings.Split(loadfifteenConv, ".")[0]
		loadfiveConv := strconv.FormatFloat(l.Load5, 'f', 6, 64)
		loadfive := strings.Split(loadfiveConv, ".")[0]

		ipaddress, err := networkip.ExternalIP()
		if err != nil {
			fmt.Println(err.Error())
		}

		// UNIX system commands
		dnsfile := exec.Command("ls", "/etc/resolv.conf")
		dnsfileout, err := dnsfile.Output()
		dnsgrep := exec.Command("grep", "nameserver", "/etc/resolv.conf")
		dnsawk := exec.Command("awk", "{print$2}")
		dnsgrepOut, err := dnsgrep.StdoutPipe()
		dnsgrep.Start()
		dnsawk.Stdin = dnsgrepOut
		dnsOut, err := dnsawk.Output()
		dnsstring := string(dnsOut)
		dnsoutputSlice := strings.Split(dnsstring, "\n")
		dnsjs, _ := json.Marshal(dnsoutputSlice)

		wbin := exec.Command("ls", "/usr/bin/w")
		wbinout, err := wbin.Output()
		wh := exec.Command("w", "-h")
		whawk := exec.Command("awk", "{print$1\"-\"$2}")
		whOut, err := wh.StdoutPipe()
		wh.Start()
		whawk.Stdin = whOut
		wOut, err := whawk.Output()
		whstring := string(wOut)
		whoutputSlice := strings.Split(whstring, "\n")
		whjs, _ := json.Marshal(whoutputSlice)

		passfile := exec.Command("ls", "/etc/passwd")
		passfileout, err := passfile.Output()
		passgrep := exec.Command("grep", "-v", "^#", "/etc/passwd")
		passgrepOut, err := passgrep.Output()
		passstring := string(passgrepOut)
		passoutputSlice := strings.Split(passstring, "\n")
		passjs, _ := json.Marshal(passoutputSlice)

		auditctlbin := exec.Command("ls", "/sbin/auditctl")
		auditctlbinout, err := auditctlbin.Output()
		auditctl := exec.Command("auditctl", "-l")
		auditctlOut, err := auditctl.Output()
		auditctlstring := string(auditctlOut)
		auditctloutputSlice := strings.Split(auditctlstring, "\n")
		auditctljs, _ := json.Marshal(auditctloutputSlice)

		rpmbin := exec.Command("ls", "/bin/rpm")
		rpmbinout, err := rpmbin.Output()
		rpmqa := exec.Command("rpm", "-qa")
		rpmsort := exec.Command("sort")
		rpmqaOut, err := rpmqa.StdoutPipe()
		rpmqa.Start()
		rpmsort.Stdin = rpmqaOut
		rpmOut, err := rpmsort.Output()
		rpmstring := string(rpmOut)
		rpmoutputSlice := strings.Split(rpmstring, "\n")
		rpmjs, _ := json.Marshal(rpmoutputSlice)

		dpkgbin := exec.Command("ls", "/usr/bin/dpkg")
		dpkgbinout, err := dpkgbin.Output()
		dpkg := exec.Command("dpkg", "-l")
		dpkgawk := exec.Command("awk", "/^[a-z]/{print$2\"-\"$3}")
		dpkglOut, err := dpkg.StdoutPipe()
		dpkg.Start()
		dpkgawk.Stdin = dpkglOut
		dpkgOut, err := dpkgawk.Output()
		dpkgstring := string(dpkgOut)
		dpkgoutputSlice := strings.Split(dpkgstring, "\n")
		dpkgjs, _ := json.Marshal(dpkgoutputSlice)

		iptablesbin := exec.Command("ls", "/sbin/iptables")
		iptablesbinout, err := iptablesbin.Output()
		iptablesl := exec.Command("iptables", "-L")
		iptablesgrep := exec.Command("grep", "-v", "^Chain\\|target\\|^$")
		iptableslOut, err := iptablesl.StdoutPipe()
		iptablesl.Start()
		iptablesgrep.Stdin = iptableslOut
		iptablesOut, err := iptablesgrep.Output()
		iptablesstring := string(iptablesOut)
		iptablesoutputSlice := strings.Split(iptablesstring, "\n")
		iptablesjs, _ := json.Marshal(iptablesoutputSlice)

		dockerbin := exec.Command("which", "docker")
		dockerbinout, err := dockerbin.Output()

		pipbin := exec.Command("which", "pip")
		pipbinout, err := pipbin.Output()
		pipfree := exec.Command("pip", "freeze")
		pipsort := exec.Command("sort")
		pipfreeOut, err := pipfree.StdoutPipe()
		pipfree.Start()
		pipsort.Stdin = pipfreeOut
		pipOut, err := pipsort.Output()
		pipstring := string(pipOut)
		pipreplace := strings.Replace(pipstring, "==", "-", -1)
		pipoutSlice := strings.Split(pipreplace, "\n")
		pipjs, _ := json.Marshal(pipoutSlice)

		gembin := exec.Command("which", "gem")
		gembinout, err := gembin.Output()
		gemlist := exec.Command("gem", "list")
		gemgrep := exec.Command("grep", "^[a-zA-Z]")
		gemlistOut, err := gemlist.StdoutPipe()
		gemlist.Start()
		gemgrep.Stdin = gemlistOut
		gemOut, err := gemgrep.Output()
		gemstring := string(gemOut)
		gemreplace := strings.Replace(gemstring, " (", "-", -1)
		gemreplace2 := strings.Replace(gemreplace, ")", "", -1)
		gemoutSlice := strings.Split(gemreplace2, "\n")
		gemjs, _ := json.Marshal(gemoutSlice)

		iproutebin := exec.Command("ls", "/sbin/ip")
		iproutebinout, err := iproutebin.Output()
		iproute := exec.Command("ip", "route")
		iprouteOut, err := iproute.Output()
		iproutestring := string(iprouteOut)
		iprouteoutputSlice := strings.Split(iproutestring, "\n")
		iproutejs, _ := json.Marshal(iprouteoutputSlice)

		kernelver := exec.Command("uname", "-r")
		kernelverout, err := kernelver.Output()
		kernelverstring := string(kernelverout)
		timezone := exec.Command("date", "+%Z")
		timezoneout, err := timezone.Output()
		timezonestring := string(timezoneout)

		hostname := exec.Command("hostname", "-f")
		hostcut := exec.Command("cut", "-d.", "-f", "2-")
		hostnameOut, err := hostname.StdoutPipe()
		hostname.Start()
		hostcut.Stdin = hostnameOut
		domainname, err := hostcut.Output()
		domainstring := string(domainname)

		if err != nil {
			fmt.Println(err.Error())
		}

		// ElasticSearch endpoint
		elastic_url := "http://" + elastic_host + ":" + elastic_port + "/" + environment + "/" + h.Hostname

		// JSON file name
		if *outputFlag != "" {
			if string(*outputFlag)[:len(string(*outputFlag))-1] != "/" {
				*outputFlag += "/"
			}
		}

		filename := *outputFlag + h.Hostname + ".json"

		// Create JSON file
		f, err := os.Create(filename)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		n, err := io.WriteString(f, "{")
		if err != nil {
			fmt.Println(n, err)
			return
		}

		if string(auditctlbinout) != "" {
			audit_rules := `
      "audit_rules": %s,`

			auditctlLine := fmt.Sprintf(audit_rules, string(auditctljs))
			auditctlReplace := strings.Replace(auditctlLine, ",\"\"]", "]", -1)
			writeAuditctl, err := io.WriteString(f, auditctlReplace)
			if err != nil {
				fmt.Println(writeAuditctl, err)
				return
			}
		}

		top := `
    "cpu_count": "%v",
    "diskfree": "%v",
    "disktotal": "%v",
    "diskused": "%v",`

		topLine := fmt.Sprintf(top, cpuCounts, diskfree, disktotal, diskusedprct)
		writeTop, err := io.WriteString(f, topLine)
		if err != nil {
			fmt.Println(writeTop, err)
			return
		}

		if string(dnsfileout) != "" {
			dns_nameserver := `
      "dns_nameserver": %s,`

			dnsLine := fmt.Sprintf(dns_nameserver, string(dnsjs))
			dnsReplace := strings.Replace(dnsLine, ",\"\"]", "]", -1)
			writeDns, err := io.WriteString(f, dnsReplace)
			if err != nil {
				fmt.Println(writeDns, err)
				return
			}
		}

		if string(dockerbinout) != "" {
			dockerRaw := `
      "docker": %s,`
			containers, _ := dockerClient.ListContainers(docker.ListContainersOptions{All: false})
			dockerString := `[`

			for _, container := range containers {
				portsRaw := `%v`
				portsString := fmt.Sprintf(portsRaw, container.Ports)
				portsReplace := strings.Replace(portsString, "{", "", -1)
				portsReplace2 := strings.Replace(portsReplace, "}", "", -1)
				portsReplace3 := strings.Replace(portsReplace2, "[", "'", -1)
				portsReplace4 := strings.Replace(portsReplace3, "]", "'", -1)
				containerString := `"%v, %v, %v",`
				dockerString += fmt.Sprintf(containerString, container.Image, container.Command, portsReplace4)
			}
			dockerString += `""]`

			dockerLine := fmt.Sprintf(dockerRaw, dockerString)
			dockerReplace := strings.Replace(dockerLine, ",\"\"]", "]", -1)
			writeDocker, err := io.WriteString(f, dockerReplace)
			if err != nil {
				fmt.Println(writeDocker, err)
				return
			}
		}

		domain := `
    "domain": "%s",`

		domainLine := fmt.Sprintf(domain, strings.TrimSpace(domainstring))
		writeDomain, err := io.WriteString(f, domainLine)
		if err != nil {
			fmt.Println(writeDomain, err)
			return
		}

		// Local AWS meta-data
		awsResponse, err := client.Get("http://169.254.169.254/latest/")
		if awsResponse != nil && awsResponse.Status == string("200 OK") {
			amiid, err := http.Get("http://169.254.169.254/latest/meta-data/ami-id")
			defer amiid.Body.Close()
			amiidOut, err := ioutil.ReadAll(amiid.Body)
			instanceid, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id")
			defer instanceid.Body.Close()
			instanceidOut, err := ioutil.ReadAll(instanceid.Body)
			instancetype, err := http.Get("http://169.254.169.254/latest/meta-data/instance-type")
			defer instancetype.Body.Close()
			instancetypeOut, err := ioutil.ReadAll(instancetype.Body)
			availabilityzone, err := http.Get("http://169.254.169.254/latest/meta-data/placement/availability-zone")
			defer availabilityzone.Body.Close()
			availabilityzoneOut, err := ioutil.ReadAll(availabilityzone.Body)
			securitygroups, err := http.Get("http://169.254.169.254/latest/meta-data/security-groups")
			defer securitygroups.Body.Close()
			securitygroupsOut, err := ioutil.ReadAll(securitygroups.Body)
			profile, err := http.Get("http://169.254.169.254/latest/meta-data/profile")
			defer profile.Body.Close()
			profileOut, err := ioutil.ReadAll(profile.Body)
			publicip, err := http.Get("http://169.254.169.254/latest/meta-data/public-ipv4")
			defer publicip.Body.Close()
			publicipOut, err := ioutil.ReadAll(publicip.Body)
			if err != nil {
				fmt.Println(err.Error())
				return
			}

			aws := `
      "ec2_ami_id": "%s",
      "ec2_availability_zone": "%s",
      "ec2_instance_id": "%s",
      "ec2_instance_type": "%s",
      "ec2_profile": "%s",
      "ec2_public_ip4": "%s",
      "ec2_security_groups": "%s",`

			awsLine := fmt.Sprintf(aws, amiidOut, availabilityzoneOut, instanceidOut, instancetypeOut, profileOut, publicipOut, securitygroupsOut)
			writeAWS, err := io.WriteString(f, awsLine)
			if err != nil {
				fmt.Println(writeAWS, err)
				return
			}
		}

		environmentOut := `
    "environment": "%s",`

		envLine := fmt.Sprintf(environmentOut, environment)
		writeEnv, err := io.WriteString(f, envLine)
		if err != nil {
			fmt.Println(writeEnv, err)
			return
		}

		if string(gembinout) != "" {
			gem := `
      "gem": %s,`

			gemLine := fmt.Sprintf(gem, string(gemjs))
			gemReplace := strings.Replace(gemLine, ",\"\"]", "]", -1)
			writeGem, err := io.WriteString(f, gemReplace)
			if err != nil {
				fmt.Println(writeGem, err)
				return
			}
		}

		if string(iptablesbinout) != "" {
			iptables := `
      "iptables": %s,`

			iptablesLine := fmt.Sprintf(iptables, string(iptablesjs))
			iptablesReplace := strings.Replace(iptablesLine, ",\"\"]", "]", -1)
			writeIptables, err := io.WriteString(f, iptablesReplace)
			if err != nil {
				fmt.Println(writeIptables, err)
				return
			}
		}

		if string(iproutebinout) != "" {
			ip_route := `
      "ip_route": %s,`

			iprouteLine := fmt.Sprintf(ip_route, string(iproutejs))
			iprouteReplace := strings.Replace(iprouteLine, ",\"\"]", "]", -1)
			writeIproute, err := io.WriteString(f, iprouteReplace)
			if err != nil {
				fmt.Println(writeIproute, err)
				return
			}
		}

		bottom := `
    "hostname": "%s",
    "ipaddress": "%s",
    "kernelversion": "%s",
    "lastrun": "%s",
    "load15": "%v",
    "load1": "%v",
    "load5": "%v",
    "memoryfree": "%v",
    "memorytotal": "%v",
    "memoryused": "%v",
    "os": "%v",`

		bottomLine := fmt.Sprintf(bottom, h.Hostname, ipaddress, strings.TrimSpace(kernelverstring), dateStamp, loadfifteen, loadone, loadfive, memfree, memtotal, memusedprct, h.OS)
		writeBottom, err := io.WriteString(f, bottomLine)
		if err != nil {
			fmt.Println(writeBottom, err)
			return
		}

		if string(rpmbinout) != "" {
			packages := `
      "packages": %s,`

			rpmLine := fmt.Sprintf(packages, string(rpmjs))
			rpmReplace := strings.Replace(rpmLine, ",\"\"]", "]", -1)
			writeRpm, err := io.WriteString(f, rpmReplace)
			if err != nil {
				fmt.Println(writeRpm, err)
				return
			}
		}

		if string(dpkgbinout) != "" {
			packages := `
      "packages": %s,`

			dpkgLine := fmt.Sprintf(packages, string(dpkgjs))
			writeDpkg, err := io.WriteString(f, dpkgLine)
			if err != nil {
				fmt.Println(writeDpkg, err)
				return
			}
		}

		if string(pipbinout) != "" {
			pip := `
      "pip": %s,`

			pipLine := fmt.Sprintf(pip, string(pipjs))
			pipReplace := strings.Replace(pipLine, ",\"\"]", "]", -1)
			writePip, err := io.WriteString(f, pipReplace)
			if err != nil {
				fmt.Println(writePip, err)
				return
			}
		}

		gonetstat4 := GOnetstat.Tcp()
		tcp4String := `[`
		for _, nettcp := range gonetstat4 {
			if nettcp.State == "LISTEN" {
				ip_port := fmt.Sprintf("%v:%v", nettcp.Ip, nettcp.Port)
				pid_program := fmt.Sprintf("%v", nettcp.Exe)
				ippidString := `"%v %v",`
				tcp4String += fmt.Sprintf(ippidString, ip_port, pid_program)
			}
		}
		tcp4String += `""]`
		tcp4Replace := strings.Replace(tcp4String, ",\"\"]", "]", -1)

		gonetstat6 := GOnetstat.Tcp6()
		tcp6String := `[`
		for _, nettcp := range gonetstat6 {
			if nettcp.State == "LISTEN" {
				ip_port := fmt.Sprintf("%v:%v", nettcp.Ip, nettcp.Port)
				pid_program := fmt.Sprintf("%v", nettcp.Exe)
				ippidString := `"%v %v",`
				tcp6String += fmt.Sprintf(ippidString, ip_port, pid_program)
			}
		}
		tcp6String += `""]`
		tcp6Replace := strings.Replace(tcp6String, ",\"\"]", "]", -1)

		beforeLast := `
    "platform": "%v",
    "platformfamily": "%v",
    "platformverison": "%v",
    "tcp4_listen": %v,
    "tcp6_listen": %v,`

		beforeLastLine := fmt.Sprintf(beforeLast, h.Platform, h.PlatformFamily, h.PlatformVersion, tcp4Replace, tcp6Replace)
		writeBeforeLast, err := io.WriteString(f, beforeLastLine)
		if err != nil {
			fmt.Println(writeBeforeLast, err)
			return
		}

		timezoneLast := `
    "timezone": "%s",
    "uptime": "%v",`

		timezoneLastLine := fmt.Sprintf(timezoneLast, strings.TrimSpace(timezonestring), time.Duration(h.Uptime)*time.Second)
		writeTimezoneLast, err := io.WriteString(f, timezoneLastLine)
		if err != nil {
			fmt.Println(writeTimezoneLast, err)
			return
		}

		if string(passfileout) != "" {
			users := `
      "users": %s,`

			usersLine := fmt.Sprintf(users, string(passjs))
			usersReplace := strings.Replace(usersLine, ",\"\"]", "]", -1)
			writeUsers, err := io.WriteString(f, usersReplace)
			if err != nil {
				fmt.Println(writeUsers, err)
				return
			}
		}

		if string(wbinout) != "" {
			users_loggedin := `
      "users_loggedin": %s,`

			whLine := fmt.Sprintf(users_loggedin, string(whjs))
			whReplace := strings.Replace(whLine, ",\"\"]", "]", -1)
			writeWh, err := io.WriteString(f, whReplace)
			if err != nil {
				fmt.Println(writeWh, err)
				return
			}
		}

		last := `
    "virtualizationrole": "%v",
    "virtualizationsystem": "%v"
    }`

		lastLine := fmt.Sprintf(last, h.VirtualizationRole, h.VirtualizationSystem)
		writeLast, err := io.WriteString(f, lastLine)
		if err != nil {
			fmt.Println(writeLast, err)
			return
		}

		f.Close()

		// Generate SHA256 SUM of JSON file
		const filechunk = 8192

		file, err := os.Open(filename)

		if err != nil {
			fmt.Println(err.Error())
			return
		}

		defer file.Close()

		info, _ := file.Stat()
		filesize := info.Size()
		blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))
		hash := sha256.New()

		for i := uint64(0); i < blocks; i++ {
			blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))
			buf := make([]byte, blocksize)

			file.Read(buf)
			io.WriteString(hash, string(buf))
		}

		if viperReadConfig != nil {
			fmt.Println(dateStamp, h.Hostname, "INFO no config file used, using default configuration")
		}

		fmt.Printf("%s %s INFO %s SHA256 checksum is %x\n", dateStamp, h.Hostname, file.Name(), hash.Sum(nil))

		// Check to see if ElasticSearch server is up
		elasticResponse, err := client.Get(elastic_url)
		if elasticResponse != nil {
			jsonStr, err := ioutil.ReadFile(filename)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			fmt.Println(dateStamp, h.Hostname, "INFO elasticsearch endpoint:", elastic_url)
			if overwrite == "enable" {
				reqDelete, err := http.NewRequest("DELETE", elastic_url, nil)
				if username != "undef" {
					reqDelete.SetBasicAuth(username, password)
				}
				respDelete, err := http.DefaultClient.Do(reqDelete)
				fmt.Println(dateStamp, h.Hostname, "DELETE elasticsearch type status:", respDelete.Status)
				if err != nil {
					fmt.Println(err.Error())
				}
			}
			reqPost, err := http.NewRequest("POST", elastic_url, bytes.NewBuffer(jsonStr))
			if password != "undef" {
				reqPost.SetBasicAuth(username, password)
			}
			reqPost.Header.Set("Content-Type", "application/json")

			clientReq := &http.Client{}
			respPost, err := clientReq.Do(reqPost)
			if err != nil {
				fmt.Println(err.Error())
			}
			defer respPost.Body.Close()
			fmt.Println(dateStamp, h.Hostname, "POST json elasticsearch type status:", respPost.Status)
			postBody, _ := ioutil.ReadAll(respPost.Body)
			fmt.Println(dateStamp, h.Hostname, "POST response body:", string(postBody))
		} else {
			fmt.Println(dateStamp, h.Hostname, "FAIL unable to connect to elasticeearch server:", "http://"+elastic_host+":"+elastic_port)
		}

		if !*daemonFlag {
			break
		}

		// Sleep / interval time for daemon
		time.Sleep(time.Duration(interval) * time.Second)

	}
}
Esempio n. 17
0
func cpuAvgInfo() (int, int) {
	avg, _ := load.LoadAvg()
	return int(avg.Load1 * 100), int(avg.Load5 * 100)
}