Skip to content

Instantly share code, notes, and snippets.

@josdirksen
Created February 8, 2016 13:54
Show Gist options
  • Save josdirksen/f2462dcaebd9d43e37b6 to your computer and use it in GitHub Desktop.
Save josdirksen/f2462dcaebd9d43e37b6 to your computer and use it in GitHub Desktop.

Revisions

  1. josdirksen created this gist Feb 8, 2016.
    42 changes: 42 additions & 0 deletions systemCommandHandlers.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    package handlers
    import (
    "net/http"
    "fmt"
    "bytes"
    "github.com/shirou/gopsutil/mem"
    "github.com/shirou/gopsutil/host"
    "io"
    )

    type SystemHandler struct {
    }

    func NewSystemHandler() *SystemHandler {
    p := new(SystemHandler)
    return p
    }

    func (sh SystemHandler) HandleCommand(cmdToExecute *Command, w http.ResponseWriter) {
    switch cmdToExecute.SlackCommand {
    case "mem" : handleMemCommand(w)
    case "host" : handleHostCommand(w)
    }
    }

    func handleHostCommand(w http.ResponseWriter) {
    var buffer bytes.Buffer

    info, _ := host.HostInfo()
    buffer.WriteString(fmt.Sprintf("Boottime: %v\n", info.BootTime))
    buffer.WriteString(fmt.Sprintf("Hostname: %v\n", info.Hostname))
    buffer.WriteString(fmt.Sprintf("Uptime: %v\n", info.Uptime))

    io.WriteString(w, buffer.String())
    }

    func handleMemCommand(w http.ResponseWriter) {
    v, _ := mem.VirtualMemory()
    var buffer bytes.Buffer
    buffer.WriteString(fmt.Sprintf(fmt.Sprintf("Total: %v, Free:%v, UsedPercent:%f\n", v.Total, v.Free, v.UsedPercent)))
    io.WriteString(w, buffer.String())
    }