|
| 1 | +/* |
| 2 | +Copyright © 2019 ServerAuth.com <[email protected]> |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | +package cmd |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "net/http" |
| 28 | + "net/url" |
| 29 | + "os" |
| 30 | + "runtime" |
| 31 | + "strconv" |
| 32 | + "strings" |
| 33 | + "time" |
| 34 | + |
| 35 | + "github.com/fatih/color" |
| 36 | + "github.com/shirou/gopsutil/cpu" |
| 37 | + "github.com/shirou/gopsutil/disk" |
| 38 | + "github.com/shirou/gopsutil/host" |
| 39 | + "github.com/shirou/gopsutil/load" |
| 40 | + |
| 41 | + "github.com/shirou/gopsutil/mem" |
| 42 | + "github.com/spf13/cobra" |
| 43 | + "github.com/spf13/viper" |
| 44 | +) |
| 45 | + |
| 46 | +var actionCmd = &cobra.Command{ |
| 47 | + Use: "monitor", |
| 48 | + Short: "Collect server metrics", |
| 49 | + Long: `Collects the latest server monitoring metrics and sends them to your ServerAuth account.`, |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + // Read in the existing accounts and get ready for adding another user |
| 52 | + viper.ReadInConfig() |
| 53 | + |
| 54 | + var accounts []Account |
| 55 | + configErr := viper.UnmarshalKey("accounts", &accounts) |
| 56 | + |
| 57 | + if configErr != nil { |
| 58 | + color.Red("There was a problem setting up the user account.\nPlease try again or contact ServerAuth for assistance.") |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + // Get the organisation id |
| 63 | + var orgId string |
| 64 | + viper.UnmarshalKey("orgid", &orgId) |
| 65 | + |
| 66 | + if len(orgId) <= 0 { |
| 67 | + color.Red("The organisation id is missing from your ServerAuth configuration.\nPlease check you've correctly configured ServerAuth on this server and try again.") |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + // Get the server api key |
| 72 | + var serverAPIKey string |
| 73 | + viper.UnmarshalKey("apikey", &serverAPIKey) |
| 74 | + |
| 75 | + if len(serverAPIKey) <= 0 { |
| 76 | + color.Red("The server API key is missing.\nPlease check you've correctly configured ServerAuth on this server and try again.") |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + // Get the team api key |
| 81 | + var teamAPIKey string |
| 82 | + viper.UnmarshalKey("teamkey", &teamAPIKey) |
| 83 | + |
| 84 | + if len(teamAPIKey) <= 0 { |
| 85 | + color.Red("The team API key is missing.\nPlease check you've correctly configured ServerAuth on this server and try again.") |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + // Get the base domain, which can optionally be overridden |
| 90 | + var baseDomain string |
| 91 | + viper.UnmarshalKey("basedomain", &baseDomain) |
| 92 | + |
| 93 | + if len(baseDomain) <= 0 { |
| 94 | + // No overridden base domain, fall back to the default |
| 95 | + baseDomain = "https://api.serverauth.com/" |
| 96 | + } |
| 97 | + |
| 98 | + // Build the base url |
| 99 | + baseURL := baseDomain + "monitoring" |
| 100 | + |
| 101 | + // Get current system stats |
| 102 | + memory, _ := mem.VirtualMemory() |
| 103 | + loadAvg, _ := load.Avg() |
| 104 | + cpuHw, _ := cpu.Info() |
| 105 | + //percent, _ := cpu.Percent(time.Second, true) |
| 106 | + uptime, _ := host.Uptime() |
| 107 | + misc, _ := load.Misc() |
| 108 | + platform, family, version, _ := host.PlatformInformation() |
| 109 | + diskStat, err := disk.Usage("/") |
| 110 | + currentTime := time.Now() |
| 111 | + timeZone, timeOffset := currentTime.Zone() |
| 112 | + |
| 113 | + // Create http client |
| 114 | + httpClient := http.Client{ |
| 115 | + Timeout: time.Second * 10, // Maximum of 10 secs |
| 116 | + } |
| 117 | + |
| 118 | + form := url.Values{} |
| 119 | + |
| 120 | + // Mem |
| 121 | + form.Add("mem[total]", fmt.Sprint(memory.Total)) |
| 122 | + form.Add("mem[free]", fmt.Sprint(memory.Free)) |
| 123 | + form.Add("mem[used]", fmt.Sprint(memory.Used)) |
| 124 | + form.Add("mem[used_percent]", fmt.Sprint(memory.UsedPercent)) |
| 125 | + |
| 126 | + // Loadavg |
| 127 | + form.Add("load[1]", fmt.Sprint(loadAvg.Load1)) |
| 128 | + form.Add("load[5]", fmt.Sprint(loadAvg.Load5)) |
| 129 | + form.Add("load[15]", fmt.Sprint(loadAvg.Load15)) |
| 130 | + |
| 131 | + // Processes |
| 132 | + form.Add("procs[running]", fmt.Sprint(misc.ProcsRunning)) |
| 133 | + form.Add("procs[blocked]", fmt.Sprint(misc.ProcsBlocked)) |
| 134 | + form.Add("procs[total]", fmt.Sprint(misc.ProcsTotal)) |
| 135 | + |
| 136 | + // Generic CPU HW |
| 137 | + form.Add("cpu", fmt.Sprint(cpuHw)) |
| 138 | + |
| 139 | + // Uptime |
| 140 | + form.Add("uptime_seconds", fmt.Sprint(uptime)) |
| 141 | + |
| 142 | + // Platform info |
| 143 | + form.Add("platform[name]", fmt.Sprint(platform)) |
| 144 | + form.Add("platform[family]", fmt.Sprint(family)) |
| 145 | + form.Add("platform[version]", fmt.Sprint(version)) |
| 146 | + |
| 147 | + // Disk info |
| 148 | + form.Add("disk[total]", strconv.FormatUint(diskStat.Total, 10)) |
| 149 | + form.Add("disk[used]", strconv.FormatUint(diskStat.Used, 10)) |
| 150 | + form.Add("disk[free]", strconv.FormatUint(diskStat.Free, 10)) |
| 151 | + form.Add("disk[percent_free]", strconv.FormatFloat(diskStat.UsedPercent, 'f', 2, 64)) |
| 152 | + form.Add("disk[inodes_total]", strconv.FormatUint(diskStat.InodesTotal, 10)) |
| 153 | + form.Add("disk[inodes_used]", strconv.FormatUint(diskStat.InodesUsed, 10)) |
| 154 | + form.Add("disk[inodes_free]", strconv.FormatUint(diskStat.InodesFree, 10)) |
| 155 | + |
| 156 | + // Time info |
| 157 | + form.Add("time[zone]", fmt.Sprint(timeZone)) |
| 158 | + form.Add("time[offset]", fmt.Sprint(timeOffset)) |
| 159 | + form.Add("time[now]", fmt.Sprint(currentTime.Unix())) |
| 160 | + |
| 161 | + // Create a request |
| 162 | + req, err := http.NewRequest(http.MethodPost, baseURL, strings.NewReader(form.Encode())) |
| 163 | + if err != nil { |
| 164 | + log.Fatal(err) |
| 165 | + } |
| 166 | + |
| 167 | + req.Header.Set("User-Agent", "ServerAuthAgent-v2.0.0;"+runtime.GOOS) |
| 168 | + req.Header.Set("TeamApiKey", teamAPIKey) |
| 169 | + req.Header.Set("ServerApiKey", serverAPIKey) |
| 170 | + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") |
| 171 | + httpClient.Do(req) |
| 172 | + }, |
| 173 | +} |
| 174 | + |
| 175 | +func init() { |
| 176 | + rootCmd.AddCommand(actionCmd) |
| 177 | +} |
0 commit comments