diff --git a/crawler/crawler.go b/crawler/crawler.go index b33a369..154f873 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -97,6 +97,23 @@ func (c *Crawler) DiskFreeStat(ch chan interface{}, keys []string) { }() } +func (c *Crawler) DiskIFreeStat(ch chan interface{}, keys []string) { + go func() { + diskFree := &stat.DiskIFreeStat{} + b, err := c.sshCli.RunCommand(diskFree.Command()) + if err != nil { + ch <- err + } + res, err := diskFree.Read(bytes.NewReader(b), keys) + if err != nil { + ch <- err + } + ch <- res + close(ch) + }() +} + + func (c *Crawler) LoadAvgStat(ch chan interface{}) { go func() { load := &stat.LoadAvg{} diff --git a/main.go b/main.go index 61020ae..7f0f35d 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,11 @@ import ( func main() { - const ip = "192.168.1.215" + const ip = "192.168.1.15" const port = "22" - const user = "snoop" + const user = "administrator" const pw = "!@#$qwer1234" - const keyFilePath = "/home/insanity/.ssh/id_rsa" + const keyFilePath = "" ///home/insanity/.ssh/id_rsa cr, err := crawler.New(ip, port, user, pw, keyFilePath) if err != nil { @@ -92,6 +92,22 @@ func main() { print(<-diskFreeCh) + //Disk Inode Free + diskInodeKeys := []string { + "disk[0].inode.usage.fs", + "disk[0].inode.usage.used", + "disk[0].inode.usage.available", + "disk[0].inode.usage.usedperc", + "disk[0].inode.usage.mounted", + + "disk[1].inode.usage.fs", + "disk[2].inode.usage.fs", + } + diskInodeFreeCh := make(chan interface{}) + cr.DiskIFreeStat(diskInodeFreeCh, diskInodeKeys) + print(<-diskInodeFreeCh) + + //Network netKeys := []string { diff --git a/stat/disk_ifree.go b/stat/disk_ifree.go new file mode 100644 index 0000000..5c7748c --- /dev/null +++ b/stat/disk_ifree.go @@ -0,0 +1,78 @@ +package stat + +import ( + "bufio" + "io" + "strings" + "strconv" + "git.loafle.net/overflow/ssh_crawler/util" +) + +type DiskIFreeStat struct { + Filesystem, + Size, + Used, + Free, + UsePerc, + MountedOn string +} + +func (diskFree DiskIFreeStat) Command() string { + return "df -ik" +} + +func (diskio DiskIFreeStat) Read(r io.Reader, keys []string) (*map[string]string, error) { + + var scanner = bufio.NewScanner(r) + var stats = []DiskIFreeStat{} + + scanner.Scan() + for scanner.Scan() { + parts := strings.Fields(scanner.Text()) + stats = append(stats, DiskIFreeStat{ + Filesystem: parts[0], + Size: parts[1], + Used: parts[2], + Free: parts[3], + UsePerc: removePercUnit(parts[4]), + MountedOn: parts[5], + }) + } + + res, err := diskio.parse(keys, stats) + if err != nil { + return nil, err + } + + return &res, scanner.Err() +} + + +func (diskio DiskIFreeStat) parse(keys []string, data []DiskIFreeStat) (map[string]string, error) { + resMap := make(map[string]string) + + for _, key := range keys { + t := strings.Split(key, ".") + suffix := t[len(t)-1] + ext := util.ExtractInBracket(key) + idx, _ := strconv.Atoi(ext) + + switch suffix { + case "fs": + resMap[key] = data[idx].Filesystem + case "used": + resMap[key] = data[idx].Used + case "free": + resMap[key] = data[idx].Free + case "usedperc": + resMap[key] = data[idx].UsePerc + case "mounted": + resMap[key] = data[idx].MountedOn + + default: + + } + } + + return resMap, nil +} \ No newline at end of file diff --git a/stat/disk_io.go b/stat/disk_io.go index 66773e2..445fa00 100644 --- a/stat/disk_io.go +++ b/stat/disk_io.go @@ -40,7 +40,7 @@ func (diskio DiskIOStat) Read(r io.Reader, keys []string) (interface{}, error) { return nil, fmt.Errorf("invalid line in %s: %s", "/proc/diskstats", scanner.Text()) } deviceName := parts[2] - if !strings.HasPrefix(deviceName, "sd") { + if strings.HasPrefix(deviceName, "loop") { continue } stats = append(stats, DiskIOStat{