|
@@ -1,93 +1,93 @@
|
1
|
|
-package main
|
2
|
|
-
|
3
|
|
-import (
|
4
|
|
- "bufio"
|
5
|
|
- "errors"
|
6
|
|
- "flag"
|
7
|
|
- "fmt"
|
8
|
|
- "jsh"
|
9
|
|
- "regexp"
|
10
|
|
- "os"
|
11
|
|
- "strconv"
|
12
|
|
-)
|
13
|
|
-
|
14
|
|
-func convertUnit(stringUnit string) (jsh.Unit, error) {
|
15
|
|
- switch stringUnit {
|
16
|
|
- case "B":
|
17
|
|
- return jsh.B, nil
|
18
|
|
- case "kB":
|
19
|
|
- return jsh.KB, nil
|
20
|
|
- case "mB":
|
21
|
|
- return jsh.MB, nil
|
22
|
|
- case "gB":
|
23
|
|
- return jsh.GB, nil
|
24
|
|
- case "tB":
|
25
|
|
- return jsh.TB, nil
|
26
|
|
- default:
|
27
|
|
- return 0, errors.New(fmt.Sprintln("Unknown unit %s", stringUnit))
|
28
|
|
- }
|
29
|
|
-}
|
30
|
|
-
|
31
|
|
-func parseLine(line string) (string, jsh.MemStat, error) {
|
32
|
|
- // Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
|
33
|
|
- // more whitespace, and possibly the unit
|
34
|
|
- lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
|
35
|
|
-
|
36
|
|
- // Parse the line, and construct a map of the valid matches
|
37
|
|
- parsedLine := lineRegex.FindStringSubmatch(line)
|
38
|
|
- names := lineRegex.SubexpNames()
|
39
|
|
- matchedVals := map[string]string{}
|
40
|
|
- for i, n := range parsedLine {
|
41
|
|
- matchedVals[names[i]] = n
|
42
|
|
- }
|
43
|
|
-
|
44
|
|
- key := matchedVals["key"]
|
45
|
|
- val, err := strconv.Atoi(matchedVals["val"])
|
46
|
|
- if err != nil {
|
47
|
|
- return "", jsh.MemStat{}, err
|
48
|
|
- }
|
49
|
|
- unit, err := convertUnit(matchedVals["unit"])
|
50
|
|
- if err != nil {
|
51
|
|
- return "", jsh.MemStat{}, err
|
52
|
|
- }
|
53
|
|
- return key, jsh.MemStat{val, unit}, nil
|
54
|
|
-}
|
55
|
|
-
|
56
|
|
-func parseMemInfo() jsh.JshOutput {
|
57
|
|
- file, err := os.Open("/proc/meminfo")
|
58
|
|
- if err != nil {
|
59
|
|
- return jsh.JshOutput{[]string{}, []error{err}}
|
60
|
|
- }
|
61
|
|
- defer file.Close()
|
62
|
|
-
|
63
|
|
- scanner := bufio.NewScanner(file)
|
64
|
|
- memInfo := make(map[string]jsh.MemStat)
|
65
|
|
- errors := []error{}
|
66
|
|
- // Read in each line of the meminfo file, and place it in the map
|
67
|
|
- for scanner.Scan() {
|
68
|
|
- key, val, err := parseLine(scanner.Text())
|
69
|
|
- if err != nil {
|
70
|
|
- errors = append(errors, err)
|
71
|
|
- }
|
72
|
|
- memInfo[key] = val
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- finalOut := jsh.JshOutput{memInfo, errors}
|
76
|
|
- return finalOut
|
77
|
|
-}
|
78
|
|
-
|
79
|
|
-func runJsonMode() {
|
80
|
|
- output := parseMemInfo()
|
81
|
|
- fmt.Println("%s", *output.ToJson())
|
82
|
|
-}
|
83
|
|
-
|
84
|
|
-func main() {
|
85
|
|
- jsonModePtr := flag.Bool("json", false, "whether to use json mode for output")
|
86
|
|
- flag.Parse()
|
87
|
|
-
|
88
|
|
- if !*jsonModePtr {
|
89
|
|
- fmt.Printf("%s", jsh.Fallback("/usr/bin/free"))
|
90
|
|
- } else {
|
91
|
|
- runJsonMode()
|
92
|
|
- }
|
93
|
|
-}
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "bufio"
|
|
5
|
+ "errors"
|
|
6
|
+ "flag"
|
|
7
|
+ "fmt"
|
|
8
|
+ "jsh"
|
|
9
|
+ "os"
|
|
10
|
+ "regexp"
|
|
11
|
+ "strconv"
|
|
12
|
+)
|
|
13
|
+
|
|
14
|
+func convertUnit(stringUnit string) (jsh.Unit, error) {
|
|
15
|
+ switch stringUnit {
|
|
16
|
+ case "B":
|
|
17
|
+ return jsh.B, nil
|
|
18
|
+ case "kB":
|
|
19
|
+ return jsh.KB, nil
|
|
20
|
+ case "mB":
|
|
21
|
+ return jsh.MB, nil
|
|
22
|
+ case "gB":
|
|
23
|
+ return jsh.GB, nil
|
|
24
|
+ case "tB":
|
|
25
|
+ return jsh.TB, nil
|
|
26
|
+ default:
|
|
27
|
+ return 0, errors.New(fmt.Sprintln("Unknown unit %s", stringUnit))
|
|
28
|
+ }
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+func parseLine(line string) (string, jsh.MemStat, error) {
|
|
32
|
+ // Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
|
|
33
|
+ // more whitespace, and possibly the unit
|
|
34
|
+ lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
|
|
35
|
+
|
|
36
|
+ // Parse the line, and construct a map of the valid matches
|
|
37
|
+ parsedLine := lineRegex.FindStringSubmatch(line)
|
|
38
|
+ names := lineRegex.SubexpNames()
|
|
39
|
+ matchedVals := map[string]string{}
|
|
40
|
+ for i, n := range parsedLine {
|
|
41
|
+ matchedVals[names[i]] = n
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ key := matchedVals["key"]
|
|
45
|
+ val, err := strconv.Atoi(matchedVals["val"])
|
|
46
|
+ if err != nil {
|
|
47
|
+ return "", jsh.MemStat{}, err
|
|
48
|
+ }
|
|
49
|
+ unit, err := convertUnit(matchedVals["unit"])
|
|
50
|
+ if err != nil {
|
|
51
|
+ return "", jsh.MemStat{}, err
|
|
52
|
+ }
|
|
53
|
+ return key, jsh.MemStat{val, unit}, nil
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+func parseMemInfo() jsh.JshOutput {
|
|
57
|
+ file, err := os.Open("/proc/meminfo")
|
|
58
|
+ if err != nil {
|
|
59
|
+ return jsh.JshOutput{[]string{}, []error{err}}
|
|
60
|
+ }
|
|
61
|
+ defer file.Close()
|
|
62
|
+
|
|
63
|
+ scanner := bufio.NewScanner(file)
|
|
64
|
+ memInfo := make(map[string]jsh.MemStat)
|
|
65
|
+ errors := []error{}
|
|
66
|
+ // Read in each line of the meminfo file, and place it in the map
|
|
67
|
+ for scanner.Scan() {
|
|
68
|
+ key, val, err := parseLine(scanner.Text())
|
|
69
|
+ if err != nil {
|
|
70
|
+ errors = append(errors, err)
|
|
71
|
+ }
|
|
72
|
+ memInfo[key] = val
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ finalOut := jsh.JshOutput{memInfo, errors}
|
|
76
|
+ return finalOut
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+func runJsonMode() {
|
|
80
|
+ output := parseMemInfo()
|
|
81
|
+ fmt.Println("%s", *output.ToJson())
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+func main() {
|
|
85
|
+ jsonModePtr := flag.Bool("json", false, "whether to use json mode for output")
|
|
86
|
+ flag.Parse()
|
|
87
|
+
|
|
88
|
+ if !*jsonModePtr {
|
|
89
|
+ fmt.Printf("%s", jsh.Fallback("/usr/bin/free"))
|
|
90
|
+ } else {
|
|
91
|
+ runJsonMode()
|
|
92
|
+ }
|
|
93
|
+}
|