github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-unicorn/lib/unicorn_memory.go (about) 1 package mpunicorn 2 3 import ( 4 "fmt" 5 6 "strings" 7 ) 8 9 func workersMemory() (string, error) { 10 out, err := pipedCommands.Output( 11 []string{"ps", "auxw"}, 12 []string{"grep", "[u]nicorn"}, 13 []string{"grep", "-v", "master"}, 14 []string{"awk", "{m+=$6*1024} END{print m;}"}, 15 ) 16 if err != nil { 17 return "", fmt.Errorf("Cannot get unicorn workers memory: %s", err) // nolint 18 } 19 return strings.Trim(string(out), "\n"), nil 20 } 21 22 func masterMemory() (string, error) { 23 out, err := pipedCommands.Output( 24 []string{"ps", "auxw"}, 25 []string{"grep", "[u]nicorn"}, 26 []string{"grep", "master"}, 27 []string{"awk", "{m+=$6*1024} END{print m;}"}, 28 ) 29 if err != nil { 30 return "", fmt.Errorf("Cannot get unicorn master memory: %s", err) // nolint 31 } 32 return strings.Trim(string(out), "\n"), nil 33 } 34 35 func workersMemoryAvg() (string, error) { 36 out, err := pipedCommands.Output( 37 []string{"ps", "auxw"}, 38 []string{"grep", "[u]nicorn"}, 39 []string{"grep", "-v", "master"}, 40 []string{"awk", "{mem=$6*1024+mem; proc++} END{print mem/proc}"}, 41 ) 42 if err != nil { 43 return "", fmt.Errorf("Cannot get unicorn memory average: %s", err) // nolint 44 } 45 return strings.Trim(string(out), "\n"), nil 46 }