예제 #1
0
파일: disk.go 프로젝트: 0-T-0/amonagent
// 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
}
예제 #2
0
파일: network.go 프로젝트: 0-T-0/amonagent
// NetworkUsage - list
func NetworkUsage() (NetworkUsageList, error) {

	netio, _ := net.NetIOCounters(true)
	time.Sleep(1000 * time.Millisecond) // Sleep 1 second to get kb/s
	netioSecondRun, _ := net.NetIOCounters(true)
	ifaces, _ := net.NetInterfaces()
	var usage NetworkUsageList

	var validInterfaces []string
	for _, iface := range ifaces {
		if !stringInSlice("loopback", iface.Flags) {
			validInterfaces = append(validInterfaces, iface.Name)
		}

	}

	for _, io := range netio {
		if stringInSlice(io.Name, validInterfaces) {

			for _, lastio := range netioSecondRun {
				if lastio.Name == io.Name {
					Inbound := lastio.BytesRecv - io.BytesRecv
					InboundKB, _ := util.ConvertBytesTo(Inbound, "kb", 0)

					Outbound := lastio.BytesSent - io.BytesSent
					OutboundKB, _ := util.ConvertBytesTo(Outbound, "kb", 0)

					n := NetworkStruct{
						Name:     io.Name,
						Inbound:  InboundKB,
						Outbound: OutboundKB,
					}

					usage = append(usage, n)
				}
			}

		}

	}

	return usage, nil

}
예제 #3
0
파일: memory.go 프로젝트: 0-T-0/amonagent
// MemoryUsage - XXX
func MemoryUsage() MemoryStruct {
	mem, _ := psmem.VirtualMemory()
	swap, _ := psmem.SwapMemory()

	TotalMB, _ := util.ConvertBytesTo(mem.Total, "mb", 0)
	FreeMB, _ := util.ConvertBytesTo(mem.Free, "mb", 0)
	UsedMB, _ := util.ConvertBytesTo(mem.Used, "mb", 0)
	UsedPercent, _ := util.FloatDecimalPoint(mem.UsedPercent, 0)
	SwapUsedMB, _ := util.ConvertBytesTo(swap.Used, "mb", 0)
	SwapTotalMB, _ := util.ConvertBytesTo(swap.Total, "mb", 0)
	SwapFreeMB, _ := util.ConvertBytesTo(swap.Free, "mb", 0)
	SwapUsedPercent, _ := util.FloatDecimalPoint(swap.UsedPercent, 0)

	m := MemoryStruct{
		UsedMB:          UsedMB,
		TotalMB:         TotalMB,
		FreeMB:          FreeMB,
		UsedPercent:     UsedPercent,
		SwapUsedMB:      SwapUsedMB,
		SwapTotalMB:     SwapTotalMB,
		SwapFreeMB:      SwapFreeMB,
		SwapUsedPercent: SwapUsedPercent,
	}

	return m
}
예제 #4
0
// Processes - get data from sysstat, format and return the result
func Processes() (ProcessesList, error) {
	c1, _ := exec.Command("pidstat", "-ruhtd").Output()

	var ps ProcessesList
	v, _ := mem.VirtualMemory()
	memoryTotalMB, _ := util.ConvertBytesTo(float64(v.Total), "mb", 0)

	// Find header and ignore
	headerRegex, _ := regexp.Compile("d+")
	pidstatOutput := string(c1)
	pidstatLines := strings.Split(pidstatOutput, "\n")

	// Helper
	// Time(0)   UID(1)      TGID(2)       TID(3)
	// %usr{4} %system{5}  %guest{6}    %CPU{7}   CPU{8}
	// minflt/s{9}  majflt/s{10}     VSZ{11}    RSS{12}
	// %MEM{13}   kB_rd/s{14}   kB_wr/s{15} kB_ccwr/s{16}  Command{17}
	var headerData []string
	for _, processLine := range pidstatLines {
		// Get the header
		if strings.Contains(processLine, "%CPU") || strings.Contains(processLine, "%Command") {
			headerData = strings.Fields(processLine)
			if len(headerData) > 0 {
				// remove the first column, if it has the # sign
				if headerData[0] == "#" {
					headerData = append(headerData[:0], headerData[0+1:]...)
				}
			}
			break
		}

	}
	for _, processLine := range pidstatLines {
		if len(headerRegex.FindString(processLine)) == 0 {
			processData := strings.Fields(processLine)

			if len(processData) == len(headerData) {
				masterthreadIDIndex := SliceFindStringIndex(headerData, "TID")
				if masterthreadIDIndex != -1 {
					masterthreadID := processData[masterthreadIDIndex]
					masterthreadIDtoINT, _ := strconv.Atoi(masterthreadID)

					// It is a master thread, proceed with the actual data
					if masterthreadIDtoINT == 0 {

						CPUIndex := SliceFindStringIndex(headerData, "%CPU")
						MEMIndex := SliceFindStringIndex(headerData, "%MEM")

						if CPUIndex != -1 && MEMIndex != -1 {
							cpuPercent := processData[CPUIndex]
							cpuPercenttoINT, _ := strconv.ParseFloat(cpuPercent, 64)

							memPercent := processData[MEMIndex]
							memPercenttoINT, _ := strconv.ParseFloat(memPercent, 64)

							var processMemoryMB, _ = util.FloatDecimalPoint(memoryTotalMB/100*memPercenttoINT, 2)

							ReadKBIndex := SliceFindStringIndex(headerData, "kB_rd/s")
							WriteKBIndex := SliceFindStringIndex(headerData, "kB_wr/s")
							var ReadKBytes = 0.0
							var WriteKBytes = 0.0

							if ReadKBIndex != -1 && WriteKBIndex != -1 {
								ReadPerSecond := processData[ReadKBIndex]
								ReadKBytes, _ = strconv.ParseFloat(ReadPerSecond, 64)

								WritePerSecond := processData[WriteKBIndex]
								WriteKBytes, _ = strconv.ParseFloat(WritePerSecond, 64)

								if ReadKBytes == -1.0 && WriteKBytes == -1.0 {
									ReadKBytes = 0.0
									WriteKBytes = 0.0
								}

							}

							processNameIndex := SliceFindStringIndex(headerData, "command")

							// Everything is find up to this point, append
							if processNameIndex != -1 {
								processName := processData[processNameIndex]

								formattedprocessMemoryMB, _ := util.FloatToString(processMemoryMB)

								c := ProcessStruct{
									CPU:     cpuPercenttoINT,
									Memory:  formattedprocessMemoryMB,
									Name:    processName,
									KBRead:  ReadKBytes,
									KBWrite: WriteKBytes,
								}

								ps = append(ps, c)

							}

						} else {
							processLogger.Info("Can't find mem/cpu data")
						}

					}
				}

			}

			//fmt.Print(headerData)

		}

	}

	return ps, nil
}