Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bingoohuang committed Jun 11, 2019
1 parent 8ab0089 commit 09a5480
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 166 deletions.
118 changes: 57 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,38 @@ Usage of ./sysinfo:
## demo:
```bash
➜ sysinfo git:(master) ✗ ./sysinfo
+----------+----------------+-------+---------+----------+--------------------------------------+------------------+----------------+
| HOSTNAME | UPTIME | PROCS | HOST OS | PLATFORM | HOSTID | PLATFORM VERSION | KERNEL VERSION |
+----------+----------------+-------+---------+----------+--------------------------------------+------------------+----------------+
| bogon | 24752(7 hours) | 409 | darwin | darwin | 7c8bb636-e593-3ce4-8528-9bd24a688851 | 10.14.5 | 18.6.0 |
+----------+----------------+-------+---------+----------+--------------------------------------+------------------+----------------+
Last login: Mon Jun 10 16:10:25 2019 from 192.168.218.9
[root@BJCA-device ~]# ./sysinfo
+---+-------------+---------+--------------+-------+-------+-------------+--------------------------------------+------------------+-----------------------+
| # | HOSTNAME | UPTIME | UPTIME HUMAN | PROCS | OS | PLATFORM | HOST ID | PLATFORM VERSION | KERNEL VERSION |
+---+-------------+---------+--------------+-------+-------+-------------+--------------------------------------+------------------+-----------------------+
| 1 | BJCA-device | 1023995 | 11 days | 215 | linux | bjca-device | fbe926a2-ade0-5bb0-9e37-04d4c439354d | 3.0 | 3.10.0-862.el7.x86_64 |
+---+-------------+---------+--------------+-------+-------+-------------+--------------------------------------+------------------+-----------------------+

+---+----------+----------+-----------------+
| # | TOTAL | FREE | USED PERCENTAGE |
+---+----------+----------+-----------------+
| 1 | 7.572GiB | 5.552GiB | 17.04% |
+---+----------+----------+-----------------+

+---+-------------+--------------+--------+-------------------------------------------+-------+------+
| # | PHYSICAL ID | VENDOR ID | FAMILY | MODEL NAME | CORES | MHZ |
+---+-------------+--------------+--------+-------------------------------------------+-------+------+
| 1 | 0 | GenuineIntel | 6 | Intel(R) Xeon(R) CPU E3-1230 v6 @ 3.50GHz | 8 | 3900 |
+---+-------------+--------------+--------+-------------------------------------------+-------+------+

+---+-------+--------------------------------------+--------+----------+----------+----------+--------------+
| # | PATH | DEVICE | FSTYPE | TOTAL | USED | FREE | USED PERCENT |
+---+-------+--------------------------------------+--------+----------+----------+----------+--------------+
| 1 | / | /dev/mapper/centos_bjca--device-root | xfs | 899.6GiB | 1.823GiB | 897.7GiB | 00.20% |
| 2 | /boot | /dev/sda1 | " | 1014MiB | 141.9MiB | 872.1MiB | 14.00% |
+---+-------+--------------------------------------+--------+----------+----------+----------+--------------+
+-----------+----------+---------------------+
| TOTAL MEM | FREE MEM | MEM USED PERCENTAGE |
+-----------+----------+---------------------+
| 16GiB | 1.293GiB | 57.05% |
+-----------+----------+---------------------+

+----------------+--------------+--------+------------------------------------------+-------+------+
| CPU PHYSICALID | VENDORID | FAMILY | MODEL NAME | CORES | MHZ |
+----------------+--------------+--------+------------------------------------------+-------+------+
| | GenuineIntel | 6 | Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz | 6 | 2200 |
+----------------+--------------+--------+------------------------------------------+-------+------+

+---+--------------+-----------------+--------+------------+-----------+-----------+-----------+
| # | DEVICE | PATH | FSTYPE | DISK TOTAL | DISK USED | DISK FREE | DISK USED |
+---+--------------+-----------------+--------+------------+-----------+-----------+-----------+
| 1 | /dev/disk1s1 | / | apfs | 233.5GiB | 144.9GiB | 86.92GiB | 62.51% |
| 2 | /dev/disk1s4 | /private/var/vm | " | " | 1GiB | " | 1.14% |
+---+--------------+-----------------+--------+------------+-----------+-----------+-----------+
+---+----------------+-------------------+----------------------------------------------+
| # | INTERFACE NAME | HARDWARE ADDR | ADDRS |
+---+----------------+-------------------+----------------------------------------------+
| 1 | en5 | ac:de:48:00:11:22 | fe80::aede:48ff:fe00:1122/64 |
| 2 | en0 | f0:18:98:a5:12:27 | fe80::49:dfcf:6bf2:7a96/64 192.168.162.95/24 |
| 3 | awdl0 | ce:f7:7f:fa:e1:70 | fe80::ccf7:7fff:fefa:e170/64 |
| 4 | en8 | 00:e0:4c:68:1d:97 | fe80::4d:5ead:7376:4e97/64 192.168.217.47/24 |
| 5 | vmnet1 | 00:50:56:c0:00:01 | 172.16.67.1/24 |
| 6 | vmnet8 | 00:50:56:c0:00:08 | 192.168.228.1/24 |
+---+----------------+-------------------+----------------------------------------------+
+---+----------------+-------------------+-----------------------------------------------+
| # | INTERFACE NAME | HARDWARE ADDR | ADDRS |
+---+----------------+-------------------+-----------------------------------------------+
| 1 | team0 | 04:d4:c4:39:35:4e | 192.168.221.46/24 fe80::6d4:c4ff:fe39:354e/64 |
+---+----------------+-------------------+-----------------------------------------------+
```
Expand Down Expand Up @@ -91,32 +87,32 @@ Last login: Mon Jun 10 15:46:17 2019 from 192.168.218.9
```bash
[footstone@fs01-192-168-126-182 ~]$ ./sysinfo
+----------------------+-------------------+-------+---------+----------+--------------------------------------+------------------+-----------------------+
| HOSTNAME | UPTIME | PROCS | HOST OS | PLATFORM | HOSTID | PLATFORM VERSION | KERNEL VERSION |
+----------------------+-------------------+-------+---------+----------+--------------------------------------+------------------+-----------------------+
| fs01-192-168-126-182 | 6073946(2 months) | 422 | linux | centos | 86f6047a-3ed8-4e0c-a486-fafd068e63ba | 7.5.1804 | 3.10.0-862.el7.x86_64 |
+----------------------+-------------------+-------+---------+----------+--------------------------------------+------------------+-----------------------+

+-----------+----------+---------------------+
| TOTAL MEM | FREE MEM | MEM USED PERCENTAGE |
+-----------+----------+---------------------+
| 62.37GiB | 24.41GiB | 19.41% |
+-----------+----------+---------------------+

+----------------+--------------+--------+-------------------------------------------+-------+------+
| CPU PHYSICALID | VENDORID | FAMILY | MODEL NAME | CORES | MHZ |
+----------------+--------------+--------+-------------------------------------------+-------+------+
| 0 | GenuineIntel | 6 | Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz | 6 | 1700 |
| 1 | " | " | " | " | " |
+----------------+--------------+--------+-------------------------------------------+-------+------+
+---+-------------------------+-------+--------+------------+-----------+-----------+-----------+
| # | DEVICE | PATH | FSTYPE | DISK TOTAL | DISK USED | DISK FREE | DISK USED |
+---+-------------------------+-------+--------+------------+-----------+-----------+-----------+
| 1 | /dev/mapper/centos-root | / | xfs | 49.98GiB | 14.13GiB | 35.85GiB | 28.27% |
| 2 | /dev/sda2 | /boot | " | 1014MiB | 168.9MiB | 845.1MiB | 16.66% |
| 3 | /dev/mapper/centos-home | /home | " | 2.098TiB | 101.8GiB | 1.998TiB | 4.74% |
+---+-------------------------+-------+--------+------------+-----------+-----------+-----------+
+---+----------------------+---------+--------------+-------+-------+----------+--------------------------------------+------------------+-----------------------+
| # | HOSTNAME | UPTIME | UPTIME HUMAN | PROCS | OS | PLATFORM | HOST ID | PLATFORM VERSION | KERNEL VERSION |
+---+----------------------+---------+--------------+-------+-------+----------+--------------------------------------+------------------+-----------------------+
| 1 | fs01-192-168-126-182 | 6149498 | 2 months | 422 | linux | centos | 86f6047a-3ed8-4e0c-a486-fafd068e63ba | 7.5.1804 | 3.10.0-862.el7.x86_64 |
+---+----------------------+---------+--------------+-------+-------+----------+--------------------------------------+------------------+-----------------------+

+---+----------+----------+-----------------+
| # | TOTAL | FREE | USED PERCENTAGE |
+---+----------+----------+-----------------+
| 1 | 62.37GiB | 21.37GiB | 19.46% |
+---+----------+----------+-----------------+

+---+-------------+--------------+--------+-------------------------------------------+-------+------+
| # | PHYSICAL ID | VENDOR ID | FAMILY | MODEL NAME | CORES | MHZ |
+---+-------------+--------------+--------+-------------------------------------------+-------+------+
| 1 | 0 | GenuineIntel | 6 | Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz | 6 | 1700 |
| 2 | 1 | " | " | " | " | " |
+---+-------------+--------------+--------+-------------------------------------------+-------+------+
+---+-------+-------------------------+--------+----------+----------+----------+--------------+
| # | PATH | DEVICE | FSTYPE | TOTAL | USED | FREE | USED PERCENT |
+---+-------+-------------------------+--------+----------+----------+----------+--------------+
| 1 | / | /dev/mapper/centos-root | xfs | 49.98GiB | 14.09GiB | 35.89GiB | 28.19% |
| 2 | /boot | /dev/sda2 | " | 1014MiB | 168.9MiB | 845.1MiB | 16.66% |
| 3 | /home | /dev/mapper/centos-home | " | 2.098TiB | 99.54GiB | 2.001TiB | 04.63% |
+---+-------+-------------------------+--------+----------+----------+----------+--------------+
+----+-----------------+-------------------+------------------------------------------------+
| # | INTERFACE NAME | HARDWARE ADDR | ADDRS |
Expand Down
13 changes: 0 additions & 13 deletions cpu.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package main

import (
"fmt"
"sort"

"github.com/jedib0t/go-pretty/table"

"github.com/shirou/gopsutil/cpu"
)

Expand All @@ -18,16 +15,6 @@ type CPUInfo struct {
Mhz float64
}

func (p TablePrinter) tableCPUInfos(cpus []CPUInfo) {
rows := make([]table.Row, len(cpus))
for i, c := range cpus {
rows[i] = table.Row{c.PhysicalID, c.VendorID, c.Family, c.ModelName, c.Cores, c.Mhz}
}

p.TableRender(table.Row{"CPU PhysicalID", "VendorID", "Family", "Model Name", "Cores", "Mhz"}, rows...)
fmt.Println()
}

/**
GetCPUInfo 获得CPU信息
https://www.cnblogs.com/emanlee/p/3587571.html
Expand Down
22 changes: 9 additions & 13 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"fmt"

"github.com/jedib0t/go-pretty/table"

"github.com/docker/go-units"
"github.com/shirou/gopsutil/disk"
)
Expand All @@ -19,16 +17,6 @@ type DiskInfo struct {
UsedPercent string
}

func (p TablePrinter) tableDiskInfos(ds []DiskInfo) {
rows := make([]table.Row, len(ds))
for i, c := range ds {
rows[i] = table.Row{i + 1, c.Device, c.Path, c.Fstype, c.Total, c.Used, c.Free, c.UsedPercent}
}

p.TableRender(table.Row{"#", "Device", "Path", "Fstype", "Disk Total", "Disk Used", "Disk Free", "Disk Used"}, rows...)
fmt.Println()
}

func GetDiskInfos() ([]DiskInfo, error) {
stats, err := disk.Partitions(false)
if err != nil {
Expand All @@ -50,9 +38,17 @@ func GetDiskInfos() ([]DiskInfo, error) {
Total: units.BytesSize(float64(diskStat.Total)),
Used: units.BytesSize(float64(diskStat.Used)),
Free: units.BytesSize(float64(diskStat.Free)),
UsedPercent: fmt.Sprintf("%.2f%%", diskStat.UsedPercent),
UsedPercent: formatPercent(diskStat.UsedPercent),
})
}

return diskInfos, nil
}

func formatPercent(percent float64) string {
up := fmt.Sprintf("%0.2f%%", percent)
if percent < 10 {
return "0" + up
}
return up
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ go 1.12

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/bingoohuang/gou v0.0.0-20190605070258-52d324f8b9f4
github.com/docker/go-units v0.4.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-openapi/strfmt v0.19.0 // indirect
github.com/jaypipes/ghw v0.0.0-20190529200504-f4fa514ff57b
github.com/jaypipes/pcidb v0.0.0-20190603110524-92fa8668ddf5 // indirect
github.com/jedib0t/go-pretty v4.2.0+incompatible
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/shirou/gopsutil v2.18.12+incompatible
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/thoas/go-funk v0.4.0
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed // indirect
howett.net/plist v0.0.0-20181124034731-591f970eefbb // indirect
)

replace github.com/tdewolff/parse => github.com/tdewolff/parse v0.0.0-20181024085210-fced451e0bed
Loading

0 comments on commit 09a5480

Please sign in to comment.