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

View File

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

View File

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

View File

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

View File

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

View File

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