Skip to content

Commit

Permalink
Merge pull request #7 from k1LoW/host-iowait
Browse files Browse the repository at this point in the history
Add /proc/stat metrics
  • Loading branch information
k1LoW authored Apr 11, 2019
2 parents 5878351 + c3146dd commit 1f3f874
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func init() {
launchCmd.Flags().BoolVarP(&nonInteractive, "non-interactive", "", false, "Disables all interactive prompting.")

launchCmd.Flags().Int32VarP(&pid, "pid", "", 0, "PID of the process")
launchCmd.Flags().StringVarP(&threshold, "threshold", "", "cpu > 10", "Threshold conditions")
launchCmd.Flags().StringVarP(&threshold, "threshold", "", "cpu > 5 || mem > 10", "Threshold conditions")
launchCmd.Flags().IntVarP(&interval, "interval", "", 5, "Interval of checking if the threshold exceeded (seconds)")
launchCmd.Flags().IntVarP(&attempts, "attempts", "", 1, "Maximum number of attempts continuously exceeding the threshold")
launchCmd.Flags().StringVarP(&command, "command", "", "", "Command to execute when the maximum number of attempts is exceeded")
Expand Down
19 changes: 5 additions & 14 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,12 @@ func optionThreshold(threshold string, pid int32, nonInteractive bool) (option,
fmt.Printf("%s ... %s\n", color.Magenta("--threshold", color.B), "Threshold conditions.")
fmt.Println("")
fmt.Printf("%s\n", color.Magenta("Available Metrics", color.B))
fmt.Printf(" %s (now:%s): %s\n", color.White("cpu"), color.Magenta(fmt.Sprintf("%f", m["cpu"])), "The percentage of the CPU time the process uses (percent).")
fmt.Printf(" %s (now:%s): %s\n", color.White("mem"), color.Magenta(fmt.Sprintf("%f", m["mem"])), "The percentage of the total RAM the process uses (percent).")
fmt.Printf(" %s (now:%s): %s\n", color.White("rss"), color.Magenta(fmt.Sprintf("%d", m["rss"])), "The non-swapped physical memory the process uses (bytes).")
fmt.Printf(" %s (now:%s): %s\n", color.White("vms"), color.Magenta(fmt.Sprintf("%d", m["vms"])), "The amount of virtual memory the process uses (bytes).")
fmt.Printf(" %s (now:%s): %s\n", color.White("swap"), color.Magenta(fmt.Sprintf("%d", m["swap"])), "The amount of memory that has been swapped out to disk the process uses (bytes).")
fmt.Printf(" %s (now:%s): %s\n", color.White("connections"), color.Magenta(fmt.Sprintf("%d", m["connections"])), "The amount of connections(TCP, UDP or UNIX) the process uses.")
if of, ok := m["open_files"]; ok {
fmt.Printf(" %s (now:%d): %s\n", color.White("open_files"), color.Magenta(fmt.Sprintf("%d", of)), "The amount of files and file discripters opend by the process.")
mlist := metrics.List()
for _, metric := range mlist {
if of, ok := m[metric.Name]; ok {
fmt.Printf(" %s (now:%s): %s\n", color.White(metric.Name), color.Magenta(fmt.Sprintf(metric.Format, of)), metric.Description)
}
}
fmt.Printf(" %s (now:%s): %s\n", color.White("host_cpu"), color.Magenta(fmt.Sprintf("%f", m["host_cpu"])), "The percentage of cpu used.")
fmt.Printf(" %s (now:%s): %s\n", color.White("host_mem"), color.Magenta(fmt.Sprintf("%f", m["host_mem"])), "The percentage of RAM used.")
fmt.Printf(" %s (now:%s): %s\n", color.White("host_swap"), color.Magenta(fmt.Sprintf("%d", m["host_swap"])), "The amount of memory that has been swapped out to disk (bytes).")
fmt.Printf(" %s (now:%s): %s\n", color.White("load1"), color.Magenta(fmt.Sprintf("%f", m["load1"])), "Load avarage for 1 minute.")
fmt.Printf(" %s (now:%s): %s\n", color.White("load5"), color.Magenta(fmt.Sprintf("%f", m["load5"])), "Load avarage for 5 minutes.")
fmt.Printf(" %s (now:%s): %s\n", color.White("load15"), color.Magenta(fmt.Sprintf("%f", m["load15"])), "Load avarage for 15 minutes.")
fmt.Printf("%s\n", color.Magenta("Available Operators", color.B))
fmt.Printf(" %s\n", "+, -, *, /, ==, !=, <, >, <=, >=, not, and, or, !, &&, ||")
fmt.Println("")
Expand Down
2 changes: 1 addition & 1 deletion cmd/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var trackCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(trackCmd)
trackCmd.Flags().Int32VarP(&pid, "pid", "", 0, "PID of the process")
trackCmd.Flags().StringVarP(&threshold, "threshold", "", "cpu > 10", "Threshold conditions")
trackCmd.Flags().StringVarP(&threshold, "threshold", "", "cpu > 5 || mem > 10", "Threshold conditions")
trackCmd.Flags().IntVarP(&interval, "interval", "", 5, "Interval of checking if the threshold exceeded (seconds)")
trackCmd.Flags().IntVarP(&attempts, "attempts", "", 1, "Maximum number of attempts continuously exceeding the threshold")
trackCmd.Flags().StringVarP(&command, "command", "", "", "Command to execute when the maximum number of attempts is exceeded")
Expand Down
56 changes: 56 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package metrics

import (
"os/exec"
"strconv"
)

type Metric struct {
Name string
Description string
Format string
}

// List ...
func List() []Metric {
return []Metric{
{"cpu", "Percentage of the CPU time the process uses (percent).", "%f"},
{"mem", "Percentage of the total RAM the process uses (percent).", "%f"},
{"rss", "Non-swapped physical memory the process uses (bytes).", "%d"},
{"vms", "Amount of virtual memory the process uses (bytes).", "%d"},
{"swap", "Amount of memory that has been swapped out to disk the process uses (bytes).", "%d"},
{"connections", "Amount of connections(TCP, UDP or UNIX) the process uses.", "%d"},
{"open_files", "Amount of files and file discripters opend by the process.", "%d"},
{"host_cpu", "Percentage of cpu used.", "%f"},
{"host_mem", "Percentage of RAM used.", "%f"},
{"host_swap", "Amount of memory that has been swapped out to disk (bytes).", "%d"},

{"user", "Percentage of CPU utilization that occurred while executing at the user level.", "%f"},
{"system", "Percentage of CPU utilization that occurred while executing at the system level.", "%f"},
{"idle", "Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.", "%f"},
{"nice", "Percentage of CPU utilization that occurred while executing at the user level with nice priority.", "%f"},
{"iowait", "Percentage of time that CPUs were idle during which the system had an outstanding disk I/O request.", "%f"},
{"irq", "Percentage of time spent by CPUs to service hardware interrupts.", "%f"},
{"softirq", "Percentage of time spent by CPUs to service software interrupts.", "%f"},
{"steal", "Percentage of time spent in involuntary wait by the virtual CPUs while the hypervisor was servicing another virtual processor.", "%f"},
{"guest", "Percentage of time spent by CPUs to run a virtual processor.", "%f"},
{"guest_nice", "Percentage of time spent by CPUs to run a virtual processor with nice priority.", "%f"},
// {"stolen", "", "%f"},

{"load1", "Load avarage for 1 minute.", "%f"},
{"load5", "Load avarage for 5 minutes.", "%f"},
{"load15", "Load avarage for 15 minutes.", "%f"},
}
}

func ClkTck() float64 {
tck := float64(128)
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
if err == nil {
i, err := strconv.ParseFloat(string(out), 64)
if err == nil {
tck = float64(i)
}
}
return tck
}
27 changes: 23 additions & 4 deletions metrics/metrics_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/shirou/gopsutil/process"
)

const calcInterval = 0

func Get(pid int32) (map[string]interface{}, error) {
p, err := process.NewProcess(pid)
if err != nil {
Expand All @@ -32,7 +34,7 @@ func Get(pid int32) (map[string]interface{}, error) {
return map[string]interface{}{}, err
}

hostCpuPercent, err := cpu.Percent(0, false)
hostCpuPercent, err := cpu.Percent(calcInterval, false)
if err != nil {
return map[string]interface{}{}, err
}
Expand All @@ -47,6 +49,12 @@ func Get(pid int32) (map[string]interface{}, error) {
return map[string]interface{}{}, err
}

ts, err := cpu.Times(false)
if err != nil {
return map[string]interface{}{}, err
}
total := ts[0].Total()

l, err := load.Avg()
if err != nil {
return map[string]interface{}{}, err
Expand All @@ -62,9 +70,20 @@ func Get(pid int32) (map[string]interface{}, error) {
"host_cpu": hostCpuPercent[0],
"host_mem": vm.UsedPercent,
"host_swap": sw.Used,
"load1": l.Load1,
"load5": l.Load5,
"load15": l.Load15,
"user": ts[0].User / total * 100,
"system": ts[0].System / total * 100,
"idle": ts[0].Idle / total * 100,
"nice": ts[0].Nice / total * 100,
"iowait": ts[0].Iowait / total * 100,
"irq": ts[0].Irq / total * 100,
"softirq": ts[0].Softirq / total * 100,
"steal": ts[0].Steal / total * 100,
"guest": ts[0].Guest / total * 100,
"guest_nice": ts[0].GuestNice / total * 100,
// "stolen": ts[0].Stolen / total * 100,
"load1": l.Load1,
"load5": l.Load5,
"load15": l.Load15,
}
return m, nil
}
23 changes: 20 additions & 3 deletions metrics/metrics_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func Get(pid int32) (map[string]interface{}, error) {
return map[string]interface{}{}, err
}

ts, err := cpu.Times(false)
if err != nil {
return map[string]interface{}{}, err
}
total := ts[0].Total()

l, err := load.Avg()
if err != nil {
return map[string]interface{}{}, err
Expand All @@ -74,9 +80,20 @@ func Get(pid int32) (map[string]interface{}, error) {
"host_cpu": hostCpuPercent[0],
"host_mem": vm.UsedPercent,
"host_swap": sw.Used,
"load1": l.Load1,
"load5": l.Load5,
"load15": l.Load15,
"user": ts[0].User / total * 100,
"system": ts[0].System / total * 100,
"idle": ts[0].Idle / total * 100,
"nice": ts[0].Nice / total * 100,
"iowait": ts[0].Iowait / total * 100,
"irq": ts[0].Irq / total * 100,
"softirq": ts[0].Softirq / total * 100,
"steal": ts[0].Steal / total * 100,
"guest": ts[0].Guest / total * 100,
"guest_nice": ts[0].GuestNice / total * 100,
// "stolen": ts[0].Stolen / total * 100,
"load1": l.Load1,
"load5": l.Load5,
"load15": l.Load15,
}
return m, nil
}

0 comments on commit 1f3f874

Please sign in to comment.