func jsonDiskInfo() []byte { type DiskStat struct { Partition disk.DiskPartitionStat Usage disk.DiskUsageStat Counters disk.DiskIOCountersStat } var disks []DiskStat partitions, _ := disk.DiskPartitions(false) c := &DiskStat{} IOCounters, _ := disk.DiskIOCounters() for _, singleDisk := range partitions { if !strings.HasPrefix(singleDisk.Device, "/") { continue } usage, _ := disk.DiskUsage(singleDisk.Mountpoint) c.Partition = singleDisk c.Usage = *usage c.Counters = IOCounters[strings.Split(singleDisk.Device, "/")[2]] disks = append(disks, *c) } jsonPartitions, _ := json.Marshal(disks) return jsonPartitions }
func (d *Du) buildData(path string) error { path = strings.TrimSpace(path) pathStat, err := os.Stat(path) if err != nil { return err } if !pathStat.IsDir() { return errors.New(fmt.Sprintf("%v is not a directory.", path)) } duStat, err := gopsutil_disk.DiskUsage(path) if err == nil { d.Data[path] = make(map[string]interface{}) d.Data[path]["Path"] = duStat.Path d.Data[path]["Total"] = duStat.Total d.Data[path]["Free"] = duStat.Free d.Data[path]["InodesTotal"] = duStat.InodesTotal d.Data[path]["InodesFree"] = duStat.InodesFree d.Data[path]["InodesUsed"] = duStat.InodesUsed d.Data[path]["Used"] = duStat.Used if duStat.InodesTotal != 0 { d.Data[path]["InodesUsedPercent"] = duStat.InodesUsedPercent } if duStat.Total != 0 { d.Data[path]["UsedPercent"] = duStat.UsedPercent } } return err }
/** read node resource usage **/ func GetNodeResource(w http.ResponseWriter, r *http.Request) { // get this node memory memory, _ := mem.VirtualMemory() // get this node cpu percent usage cpu_percent, _ := cpu.CPUPercent(time.Duration(1)*time.Second, false) // Disk mount Point disk_partitions, _ := disk.DiskPartitions(true) // Disk usage var disk_usages []*disk.DiskUsageStat for _, disk_partition := range disk_partitions { if disk_partition.Mountpoint == "/" || disk_partition.Mountpoint == "/home" { disk_stat, _ := disk.DiskUsage(disk_partition.Device) disk_usages = append(disk_usages, disk_stat) } } // Network network, _ := net.NetIOCounters(false) // create new node obj with resource usage information node_metric := thoth.NodeMetric{ Cpu: cpu_percent, Memory: memory, DiskUsage: disk_usages, Network: network, } node_json, err := json.MarshalIndent(node_metric, "", "\t") if err != nil { fmt.Println("error:", err) } fmt.Fprint(w, string(node_json)) }
func RecordDiskUsage(path string) { c := time.Tick(30 * time.Second) for now := range c { usage, err := disk.DiskUsage(path) if err != nil { log.Println(err) } fmt.Printf("disk size %d bytes at %d seconds\n", usage.Used, now.Unix()) } }
func updateDisk() { disks, err = disk.DiskPartitions(true) log.Printf("updateDisk(): %v", spew.Sdump(disks)) if err != nil { log.Fatal(err) } disksUsage, err = disk.DiskUsage("/") log.Printf("updateDisk(): %v", spew.Sdump(disksUsage)) if err != nil { log.Fatal(err) } }
// DiskUsage - return a list with disk usage structs func DiskUsage() (DiskUsageList, error) { parts, err := disk.DiskPartitions(false) if err != nil { diskLogger.Errorf("Error getting disk usage info: %v", err) } var usage DiskUsageList for _, p := range parts { if _, err := os.Stat(p.Mountpoint); err == nil { du, err := disk.DiskUsage(p.Mountpoint) if err != nil { diskLogger.Errorf("Error getting disk usage for Mount: %v", err) } if !isPseudoFS(du.Fstype) && !removableFs(du.Path) { TotalMB, _ := util.ConvertBytesTo(du.Total, "mb", 0) FreeMB, _ := util.ConvertBytesTo(du.Free, "mb", 0) UsedMB, _ := util.ConvertBytesTo(du.Used, "mb", 0) UsedPercent := 0.0 if TotalMB > 0 && UsedMB > 0 { UsedPercent = (float64(du.Used) / float64(du.Total)) * 100.0 UsedPercent, _ = util.FloatDecimalPoint(UsedPercent, 2) DeviceName := strings.Replace(p.Device, "/dev/", "", -1) TotalMBFormatted, _ := util.FloatToString(TotalMB) FreeMBFormatted, _ := util.FloatToString(FreeMB) UsedMBFormatted, _ := util.FloatToString(UsedMB) d := DiskUsageStruct{ Name: DeviceName, Path: du.Path, Fstype: du.Fstype, Total: TotalMBFormatted, Free: FreeMBFormatted, Used: UsedMBFormatted, UsedPercent: UsedPercent, } usage = append(usage, d) } } } } return usage, err }
func (s *systemPS) DiskUsage( mountPointFilter []string, fstypeExclude []string, ) ([]*disk.DiskUsageStat, error) { parts, err := disk.DiskPartitions(true) if err != nil { return nil, err } // Make a "set" out of the filter slice mountPointFilterSet := make(map[string]bool) for _, filter := range mountPointFilter { mountPointFilterSet[filter] = true } fstypeExcludeSet := make(map[string]bool) for _, filter := range fstypeExclude { fstypeExcludeSet[filter] = true } var usage []*disk.DiskUsageStat for _, p := range parts { if len(mountPointFilter) > 0 { // If the mount point is not a member of the filter set, // don't gather info on it. _, ok := mountPointFilterSet[p.Mountpoint] if !ok { continue } } if _, err := os.Stat(p.Mountpoint); err == nil { du, err := disk.DiskUsage(p.Mountpoint) if err != nil { return nil, err } // If the mount point is a member of the exclude set, // don't gather info on it. _, ok := fstypeExcludeSet[p.Fstype] if ok { continue } du.Fstype = p.Fstype usage = append(usage, du) } } return usage, nil }
func initDiskInfo(monitorData *monitoringData) error { partitions, err := disk.DiskPartitions(true) monitorData.DiskStats = make(map[string]disk.DiskUsageStat) if err == nil { for d := range partitions { if partitions[d].Device != "none" { usage, _ := disk.DiskUsage(partitions[d].Mountpoint) if usage != nil { if !math.IsNaN(usage.UsedPercent) { monitorData.DiskStats[partitions[d].Device] = *usage } } } } } return err }
func (s *systemPS) DiskUsage() ([]*disk.DiskUsageStat, error) { parts, err := disk.DiskPartitions(true) if err != nil { return nil, err } var usage []*disk.DiskUsageStat for _, p := range parts { if _, err := os.Stat(p.Mountpoint); err == nil { du, err := disk.DiskUsage(p.Mountpoint) if err != nil { return nil, err } du.Fstype = p.Fstype usage = append(usage, du) } } return usage, nil }
func getDiskUsage() (total uint64, used uint64, usedPercent float64, err error) { if partitions, pErr := disk.DiskPartitions(true); pErr == nil { for _, partition := range partitions { if diskStat, statErr := disk.DiskUsage(partition.Mountpoint); statErr == nil { total += diskStat.Total used += diskStat.Used } else { log.Error("Failed to get diskStat:", statErr) err = statErr } } if total > 0 { usedPercent = float64(used) / float64(total) * 100 } } else { log.Error("Failed to get partitions:", pErr) err = pErr } return }
func (c *Config) DiskCheck() error { du, err := disk.DiskUsage(c.Disk.Path) if err != nil { return err } if du.UsedPercent > c.Disk.UsedPercent { td, err := NewTextDetail("disk used " + strconv.Itoa(int(du.UsedPercent)) + "%") if err != nil { return err } if err := c.SetText(td); err != nil { return err } if err := c.SlackRequest(); err != nil { return err } } if du.InodesUsedPercent > c.Disk.InodesUsedPercent { td, err := NewTextDetail("disk inode used " + strconv.Itoa(int(du.InodesUsedPercent)) + "%") if err != nil { return err } if err := c.SetText(td); err != nil { return err } if err := c.SlackRequest(); err != nil { return err } } return nil }
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) } }
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))))) } } } }
func (o *DiskMetric) Value() (float64, error) { d, e := disk.DiskUsage(*cliDiskDir) return d.UsedPercent, e }
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 "欢迎来到未知领域(ゝ∀・)" } }
func main() { for { // Docker client remote API endpoint := "unix:///var/run/docker.sock" dockerClient, _ := docker.NewClient(endpoint) // Configuration file settings using key-value viper.SetConfigName("docker-balancer") viper.AddConfigPath("/opt/docker-balancer/conf") err := viper.ReadInConfig() if err != nil { fmt.Println("No Configuration File Using DEFAULTS") } // Default settings if no config file is supplied viper.SetDefault("docker_balancer", "localhost") viper.SetDefault("docker_balancer_port", "8888") viper.SetDefault("interval", "300") viper.SetDefault("username", "undef") viper.SetDefault("password", "undef") docker_balancer := viper.GetString("docker_balancer") docker_balancer_port := viper.GetString("docker_balancer_port") interval := viper.GetInt("interval") username := viper.GetString("username") password := viper.GetString("password") transport := http.Transport{ Dial: dialTimeout, } client := http.Client{ Transport: &transport, } v, _ := mem.VirtualMemory() k, _ := disk.DiskUsage("/") h, _ := host.HostInfo() memusedprctConv := strconv.FormatFloat(v.UsedPercent, 'f', 6, 64) memusedprct := strings.Split(memusedprctConv, ".")[0] diskusedprctConv := strconv.FormatFloat(k.UsedPercent, 'f', 6, 64) diskusedprct := strings.Split(diskusedprctConv, ".")[0] timeN := time.Now() dateStamp := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", timeN.Year(), timeN.Month(), timeN.Day(), timeN.Hour(), timeN.Minute(), timeN.Second()) if err != nil { fmt.Println(err.Error()) } containers, _ := dockerClient.ListContainers(docker.ListContainersOptions{All: false}) dockerCount := 0 for container := range containers { _ = container dockerCount += 1 } dockerConv := strconv.Itoa(dockerCount) fmt.Sprintf(dockerConv) // docker-balancer endpoint and request docker_balancer_url := "http://" + docker_balancer + ":" + docker_balancer_port docker_balancer_request := "http://" + docker_balancer + ":" + docker_balancer_port + "/api/" + memusedprct + "/" + dockerConv + "/" + diskusedprct balancerResponse, err := client.Get(docker_balancer_url) if balancerResponse != nil { if err != nil { fmt.Println(err.Error()) return } // Check to see if docker-balancer server is up fmt.Println(dateStamp, h.Hostname, "INFO docker-balancer request:", docker_balancer_request) reqPost, err := http.NewRequest("POST", docker_balancer_request, nil) if password != "undef" { reqPost.SetBasicAuth(username, password) } 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 docker-balancer 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 docker-balancer endpoint:", "http://"+docker_balancer+":"+docker_balancer_port) } // Sleep time for, for loop time.Sleep(time.Duration(interval) * time.Second) } }
func GetDiskStat() SingleStat { path := "/" diskUsage, _ := disk.DiskUsage(path) return BuildDiskStat(*diskUsage) }