This commit is contained in:
insanity 2017-11-08 11:43:25 +09:00
parent 2d3723fb27
commit 192f72ff83
6 changed files with 59 additions and 60 deletions

View File

@ -19,8 +19,6 @@ func New(ip, port, user, pw string, keyFilePath string) (*Crawler, error) {
return c, nil return c, nil
} }
func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error { func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
client, err := ssh.New(ip, port, user, pw, keyFilePath) client, err := ssh.New(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
@ -31,6 +29,7 @@ func (c *Crawler) connectSSH(ip, port, user, pw, keyFilePath string) error {
return nil return nil
} }
func (c *Crawler) CPUStat(ch chan interface{}, keys []string) { func (c *Crawler) CPUStat(ch chan interface{}, keys []string) {
go func() { go func() {
cpu := &stat.CPUStat{} cpu := &stat.CPUStat{}

10
main.go
View File

@ -8,11 +8,11 @@ import (
func main() { func main() {
const ip = "192.168.1.15" const ip = "192.168.1.209"
const port = "22" const port = "22"
const user = "administrator" const user = "snoop"
const pw = "!@#$qwer1234" const pw = "qwe123"
const keyFilePath = "" ///home/insanity/.ssh/id_rsa const keyFilePath = "" // /home/insanity/.ssh/id_rsa
cr, err := crawler.New(ip, port, user, pw, keyFilePath) cr, err := crawler.New(ip, port, user, pw, keyFilePath)
if err != nil { if err != nil {
@ -157,7 +157,9 @@ func main() {
func print(data interface{}) { func print(data interface{}) {
jsonData, err := json.Marshal(data) jsonData, err := json.Marshal(data)
if err != nil { if err != nil {
fmt.Errorf("%s\n", err)
} }
fmt.Println(string(jsonData)) fmt.Println(string(jsonData))
} }

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"strings" "strings"
"git.loafle.net/overflow/ssh_crawler/util" "git.loafle.net/overflow/ssh_crawler/util"
"strconv"
) )
type CPUStat struct { type CPUStat struct {
@ -19,7 +20,7 @@ type CPUStat struct {
Steal, // (over 2.6.11) Steal, // (over 2.6.11)
Guest, // (over 2.6.24) Guest, // (over 2.6.24)
GuestNice, //(over 2.6.33) GuestNice, //(over 2.6.33)
Sum float64 Sum int64
} }
func (cpu CPUStat) Command() string { func (cpu CPUStat) Command() string {
@ -39,26 +40,26 @@ func (cpu CPUStat) Read(r io.Reader, keys []string) (*map[string]string, error)
continue continue
} }
var steal, guest, guestNice float64 var steal, guest, guestNice int64
if len(parts) > 8 { if len(parts) > 8 {
steal = util.StringToFloat64(parts[8]) steal = util.StringToInt64(parts[8])
} }
if len(parts) > 9 { if len(parts) > 9 {
guest = util.StringToFloat64(parts[9]) guest = util.StringToInt64(parts[9])
} }
if len(parts) > 10 { if len(parts) > 10 {
guestNice = util.StringToFloat64(parts[10]) guestNice = util.StringToInt64(parts[10])
} }
stats = append(stats, CPUStat{ stats = append(stats, CPUStat{
Device: util.StringToFloat64(parts[0]), Device: util.StringToInt64(parts[0]),
User: util.StringToFloat64(parts[1]), User: util.StringToInt64(parts[1]),
Nice: util.StringToFloat64(parts[2]), Nice: util.StringToInt64(parts[2]),
System: util.StringToFloat64(parts[3]), System: util.StringToInt64(parts[3]),
Idle: util.StringToFloat64(parts[4]), Idle: util.StringToInt64(parts[4]),
Iowait: util.StringToFloat64(parts[5]), Iowait: util.StringToInt64(parts[5]),
Irq: util.StringToFloat64(parts[6]), Irq: util.StringToInt64(parts[6]),
SoftIrq: util.StringToFloat64(parts[7]), SoftIrq: util.StringToInt64(parts[7]),
Steal: steal, Steal: steal,
Guest: guest, Guest: guest,
GuestNice: guestNice, GuestNice: guestNice,
@ -84,34 +85,33 @@ func (cpu CPUStat) parse(keys []string, data []CPUStat) (map[string]string, erro
} }
func (cpu CPUStat) calc(key string, d CPUStat) string { func (cpu CPUStat) calc(key string, d CPUStat) string {
var value float64 = 0 var value int64 = 0
sum := d.User + d.Nice + d.System + d.Idle + d.Iowait + d.Irq + d.SoftIrq + d.Steal + d.Guest + d.GuestNice sum := d.User + d.Nice + d.System + d.Idle + d.Iowait + d.Irq + d.SoftIrq + d.Steal + d.Guest + d.GuestNice
switch key { switch key {
case "cpu.usage.sum": case "cpu.usage.sum":
value = sum value = sum
case "cpu.usage.user": case "cpu.usage.user":
value = d.User / sum * 100 value = d.User
case "cpu.usage.nice": case "cpu.usage.nice":
value = d.Nice / sum * 100 value = d.Nice
case "cpu.usage.system": case "cpu.usage.system":
value = d.System / sum * 100 value = d.System
case "cpu.usage.idle": case "cpu.usage.idle":
value = d.Idle / sum * 100 value = d.Idle
case "cpu.usage.iowait": case "cpu.usage.iowait":
value = d.Iowait / sum * 100 value = d.Iowait
case "cpu.usage.irq": case "cpu.usage.irq":
value = d.Irq / sum * 100 value = d.Irq
case "cpu.usage.softirq": case "cpu.usage.softirq":
value = d.SoftIrq / sum * 100 value = d.SoftIrq
case "cpu.usage.steal": case "cpu.usage.steal":
value = d.Steal / sum * 100 value = d.Steal
case "cpu.usage.guest": case "cpu.usage.guest":
value = d.Guest / sum * 100 value = d.Guest
case "cpu.usage.gnice": case "cpu.usage.gnice":
value = d.GuestNice / sum * 100 value = d.GuestNice
default: default:
} }
return strconv.FormatInt(value, 10)
return util.Float64ToString(value)
} }

View File

@ -88,7 +88,7 @@ func (diskio DiskIOStat) parse(keys []string, data []DiskIOStat) (map[string]str
case "merged_readcnt": case "merged_readcnt":
resMap[key] = data[idx].ReadMerged resMap[key] = data[idx].ReadMerged
case "readbytes": case "readbytes":
resMap[key] = util.Float64ToString(util.StringToFloat64(data[idx].SectorRead) * 512) //resMap[key] = util.Float64ToString(util.StringToFloat64(data[idx].SectorRead) * 512)
case "readtime": case "readtime":
resMap[key] = data[idx].TimeSpentReading resMap[key] = data[idx].TimeSpentReading
case "writecnt": case "writecnt":
@ -96,7 +96,7 @@ func (diskio DiskIOStat) parse(keys []string, data []DiskIOStat) (map[string]str
case "merged_writecnt": case "merged_writecnt":
resMap[key] = data[idx].WritesMerged resMap[key] = data[idx].WritesMerged
case "writebytes": case "writebytes":
resMap[key] = util.Float64ToString(util.StringToFloat64(data[idx].SectorsWritten) * 512) //resMap[key] = util.Float64ToString(util.StringToFloat64(data[idx].SectorsWritten) * 512)
case "writetime": case "writetime":
resMap[key] = data[idx].TimeSpentWriting resMap[key] = data[idx].TimeSpentWriting
case "iotime": case "iotime":

View File

@ -98,42 +98,46 @@ func (net *NetDevStat) parse(keys []string, data []NetDevStat) (map[string]strin
suffix := t[len(t)-1] suffix := t[len(t)-1]
ext := util.ExtractInBracket(key) ext := util.ExtractInBracket(key)
idx, _ := strconv.Atoi(ext) idx, _ := strconv.Atoi(ext)
var regex = regexp.MustCompile(`\[(.*?)\]`)
rk := regex.ReplaceAllString(key, ``)
switch suffix { switch suffix {
case "iface": case "iface":
resMap[key] = data[idx].Iface resMap[rk] = data[idx].Iface
case "recv_byte": case "recv_byte":
resMap[key] = data[idx].RecvByte resMap[rk] = data[idx].RecvByte
case "recv_packet": case "recv_packet":
resMap[key] = data[idx].RecvPacket resMap[rk] = data[idx].RecvPacket
case "recv_err": case "recv_err":
resMap[key] = data[idx].RecvErr resMap[rk] = data[idx].RecvErr
case "recv_drop": case "recv_drop":
resMap[key] = data[idx].RecvDrop resMap[rk] = data[idx].RecvDrop
case "recv_fifo": case "recv_fifo":
resMap[key] = data[idx].RecvFifo resMap[rk] = data[idx].RecvFifo
case "recv_frame": case "recv_frame":
resMap[key] = data[idx].RecvFrame resMap[rk] = data[idx].RecvFrame
case "recv_compressed": case "recv_compressed":
resMap[key] = data[idx].RecvCompressed resMap[rk] = data[idx].RecvCompressed
case "recv_multicast": case "recv_multicast":
resMap[key] = data[idx].RecvMulticast resMap[rk] = data[idx].RecvMulticast
case "send_byte": case "send_byte":
resMap[key] = data[idx].TransByte resMap[rk] = data[idx].TransByte
case "send_packet": case "send_packet":
resMap[key] = data[idx].TransPacket resMap[rk] = data[idx].TransPacket
case "send_err": case "send_err":
resMap[key] = data[idx].TransErr resMap[rk] = data[idx].TransErr
case "send_drop": case "send_drop":
resMap[key] = data[idx].TransDrop resMap[rk] = data[idx].TransDrop
case "send_fifo": case "send_fifo":
resMap[key] = data[idx].TransFifo resMap[rk] = data[idx].TransFifo
case "send_frame": case "send_frame":
resMap[key] = data[idx].TransFrame resMap[rk] = data[idx].TransFrame
case "send_compressed": case "send_compressed":
resMap[key] = data[idx].TransCompressed resMap[rk] = data[idx].TransCompressed
case "send_multicast": case "send_multicast":
resMap[key] = data[idx].TransMulticast resMap[rk] = data[idx].TransMulticast
default: default:
} }

View File

@ -1,27 +1,21 @@
package util package util
import ( import (
"fmt"
"math" "math"
"strconv" "strconv"
"regexp" "regexp"
) )
func StringToFloat64(str string) float64 { func StringToInt64(str string) int64 {
n, _ := strconv.ParseFloat(str, 64) n, _ := strconv.ParseInt(str, 10,64)
return n return n
} }
func Float64ToString(num float64) string {
return fmt.Sprintf("%v", toFixed(num, 2))
}
func round(num float64) int { func round(num float64) int {
return int(num + math.Copysign(0.5, num)) return int(num + math.Copysign(0.5, num))
} }
func toFixed(num float64, precision int) float64 { func ToFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision)) output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output return float64(round(num*output)) / output
} }