Skip to content

مكتبة gopsutils لمعلومات النظام

المستودع: shirou/gopsutil: psutil for golang (github.com)

الوثائق: gopsutil package - github.com/shirou/gopsutil/v3 - Go Packages

في التطوير اليومي، غالبًا ما نحتاج إلى الحصول على معلومات النظام الأساسي الذي يعمل عليه البرنامج، وخلال هذه العملية نتعرض لاستدعاءات نظام مختلفة لأنظمة تشغيل مختلفة، ولإجراء التكيف نحتاج إلى الكثير من العمل والوقت، بينما gopsutils هي مكتبة معلومات نظام مطورة بلغة Go، وهي متوافقة أساسًا مع العديد من أنظمة التشغيل الرئيسية، وتدعم حاليًا البنى التالية:

  • FreeBSD i386/amd64/arm
  • Linux i386/amd64/arm(raspberry pi)
  • Windows i386/amd64/arm/arm64
  • Darwin amd64/arm64
  • OpenBSD amd64 (شكرًا @mpfz0r!)
  • Solaris amd64 (طُوّر واختُبر على SmartOS/Illumos، شكرًا @jen20!)

دعم جزئي:

  • CPU على DragonFly BSD
  • host على Linux RISC-V

كما تدعم هذه الأداة أيضًا الحصول على معلومات نظام حاويات Docker.

التثبيت

استخدم أمر go get للتثبيت

go get github.com/shirou/gopsutil/v3

TIP

الكاتب يكتب هذا المقال على نظام Win10، والنتائج ستختلف باختلاف الأنظمة.

المضيف

يتم توفير واجهات برمجة التطبيقات المتعلقة بالمضيف بواسطة حزمة host، وهي متكيفة مع أنظمة تشغيل مختلفة.

المعلومات

go
func Info() (*InfoStat, error)
go
func main() {
  info, _ := host.Info()
  JsonPrint(info)
}
json
{
  "hostname": "LAPTOP-8C92S0HL",
  "uptime": 490854,
  "bootTime": 1683909241,
  "procs": 195,
  "os": "windows",
  "platform": "Microsoft Windows 10 Home China",
  "platformFamily": "Standalone Workstation",
  "platformVersion": "10.0.19044.2251 Build 19044.2251",
  "kernelVersion": "10.0.19044.2251 Build 19044.2251",
  "kernelArch": "x86_64",
  "virtualizationSystem": "",
  "virtualizationRole": "",
  "hostId": "ba697a8c-c555-4329-905c-adc6cb650dde"
}

واجهات برمجة التطبيقات الأخرى في هذه الحزمة هي تفصيل لـ Info()، ولن يتم عرضها هنا، يرجى التعرف عليها بنفسك.

CPU

يتم توفير المعلومات المتعلقة بالمعالج بواسطة حزمة cpu، والتي توفر بشكل رئيسي 4 دوال، تستخدم للحصول على معلومات المعالج، وعدد النوى، ومعدل الاستخدام، وشرائح الوقت، والعرض بتنسيق JSON يكون أكثر ودية.

المعلومات

go
func Info() ([]InfoStat, error)
go
func main() {
  info, _ := cpu.Info()
  JsonPrint(info)
}
json
[
  {
    "cpu": 0,
    "vendorId": "GenuineIntel",
    "family": "198",
    "model": "",
    "stepping": 0,
    "physicalId": "BFEBFBFF000806EC",
    "coreId": "",
    "cores": 8,
    "modelName": "Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz",
    "mhz": 1992,
    "cacheSize": 0,
    "flags": [],
    "microcode": ""
  }
]

عدد النوى

go
func Counts(logical bool) (int, error)
go
func main() {
   cores, _ := cpu.Counts(true)
   JsonPrint(cores)
}
8

معدل الاستخدام

go
func Percent(interval time.Duration, percpu bool) ([]float64, error)
go
func main() {
   percents, _ := cpu.Percent(time.Second*5, true)
   JsonPrint(percents)
}
json
[
  4.643962848297214, 4.049844236760125, 5.607476635514018, 5.29595015576324,
  3.115264797507788, 2.803738317757009, 3.115264797507788, 2.1806853582554515
]

شرائح الوقت

go
func Times(percpu bool) ([]TimesStat, error)
go
func main() {
  times, _ := cpu.Times(true)
  JsonPrint(times)
}
json
[
  {
    "cpu": "cpu0",
    "user": 250.28125,
    "system": 176.828125,
    "idle": 2537.96875,
    "nice": 0,
    "iowait": 0,
    "irq": 11.453125,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu1",
    "user": 180.421875,
    "system": 115.078125,
    "idle": 2669.3125,
    "nice": 0,
    "iowait": 0,
    "irq": 4.125,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu2",
    "user": 298.171875,
    "system": 143.46875,
    "idle": 2523.171875,
    "nice": 0,
    "iowait": 0,
    "irq": 2.96875,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu3",
    "user": 234.890625,
    "system": 134.3125,
    "idle": 2595.609375,
    "nice": 0,
    "iowait": 0,
    "irq": 1.53125,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu4",
    "user": 249.78125,
    "system": 122.609375,
    "idle": 2592.421875,
    "nice": 0,
    "iowait": 0,
    "irq": 2.25,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu5",
    "user": 262.875,
    "system": 162.546875,
    "idle": 2539.390625,
    "nice": 0,
    "iowait": 0,
    "irq": 1.90625,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu6",
    "user": 271.296875,
    "system": 122.40625,
    "idle": 2571.109375,
    "nice": 0,
    "iowait": 0,
    "irq": 2.125,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  },
  {
    "cpu": "cpu7",
    "user": 239.328125,
    "system": 104.03125,
    "idle": 2621.4375,
    "nice": 0,
    "iowait": 0,
    "irq": 1.875,
    "softirq": 0,
    "steal": 0,
    "guest": 0,
    "guestNice": 0
  }
]

القرص

الدوال المتعلقة بالقرص يتم توفيرها بواسطة حزمة disk، وبعضها مصمم خصيصًا لنظام Linux.

معدل الاستخدام

go
func Usage(path string) (*UsageStat, error)
go
func main() {
  use, _ := disk.Usage("/")
  JsonPrint(use)
}
json
{
  "path": "/",
  "fstype": "",
  "total": 274152288256,
  "free": 251611193344,
  "used": 22541094912,
  "usedPercent": 8.22210715635224,
  "inodesTotal": 0,
  "inodesUsed": 0,
  "inodesFree": 0,
  "inodesUsedPercent": 0
}

حالة الأقسام

go
func Partitions(all bool) ([]PartitionStat, error)
go
func main() {
  part, _ := disk.Partitions(true)
  JsonPrint(part)
}
json
[
  {
    "device": "C:",
    "mountpoint": "C:",
    "fstype": "NTFS",
    "opts": ["rw", "compress"]
  },
  {
    "device": "D:",
    "mountpoint": "D:",
    "fstype": "NTFS",
    "opts": ["rw", "compress"]
  },
  {
    "device": "E:",
    "mountpoint": "E:",
    "fstype": "FAT32",
    "opts": ["rw"]
  }
]

الإدخال/الإخراج

go
func IOCounters(names ...string) (map[string]IOCountersStat, error)
go
func main() {
  part, _ := disk.IOCounters("/")
  JsonPrint(part)
}
json
{
  "C:": {
    "readCount": 435914,
    "mergedReadCount": 0,
    "writeCount": 640313,
    "mergedWriteCount": 0,
    "readBytes": 10356509696,
    "writeBytes": 16957500928,
    "readTime": 209,
    "writeTime": 127,
    "iopsInProgress": 0,
    "ioTime": 0,
    "weightedIO": 0,
    "name": "C:",
    "serialNumber": "",
    "label": ""
  },
  "D:": {
    "readCount": 65907,
    "mergedReadCount": 0,
    "writeCount": 12141,
    "mergedWriteCount": 0,
    "readBytes": 3079766528,
    "writeBytes": 518916608,
    "readTime": 24,
    "writeTime": 7,
    "iopsInProgress": 0,
    "ioTime": 0,
    "weightedIO": 0,
    "name": "D:",
    "serialNumber": "",
    "label": ""
  }
}

الذاكرة

يتم توفير واجهات برمجة التطبيقات المتعلقة بالذاكرة بواسطة حزمة mem

المعلومات

go
func VirtualMemory() (*VirtualMemoryStat, error)
go
func main() {
  memory, _ := mem.VirtualMemory()
  JsonPrint(memory)
}
json
{
  "total": 17018114048,
  "available": 5477023744,
  "used": 11541090304,
  "usedPercent": 67,
  "free": 5477023744,
  "active": 0,
  "inactive": 0,
  "wired": 0,
  "laundry": 0,
  "buffers": 0,
  "cached": 0,
  "writeBack": 0,
  "dirty": 0,
  "writeBackTmp": 0,
  "shared": 0,
  "slab": 0,
  "sreclaimable": 0,
  "sunreclaim": 0,
  "pageTables": 0,
  "swapCached": 0,
  "commitLimit": 0,
  "committedAS": 0,
  "highTotal": 0,
  "highFree": 0,
  "lowTotal": 0,
  "lowFree": 0,
  "swapTotal": 0,
  "swapFree": 0,
  "mapped": 0,
  "vmallocTotal": 0,
  "vmallocUsed": 0,
  "vmallocChunk": 0,
  "hugePagesTotal": 0,
  "hugePagesFree": 0,
  "hugePagesRsvd": 0,
  "hugePagesSurp": 0,
  "hugePageSize": 0
}

الذاكرة التبادلية

go
func SwapMemory() (*SwapMemoryStat, error)
go
func main() {
  memory, _ := mem.SwapMemory()
  JsonPrint(memory)
}
json
{
  "total": 19568250880,
  "used": 12943368192,
  "free": 6624882688,
  "usedPercent": 66.14473757196637,
  "sin": 0,
  "sout": 0,
  "pgIn": 0,
  "pgOut": 0,
  "pgFault": 0,
  "pgMajFault": 0
}

أجهزة التبديل

go
func SwapDevices() ([]*SwapDevice, error)
go
func main() {
  devices, _ := mem.SwapDevices()
  JsonPrint(devices)
}
json
[
  {
    "name": "C:\\pagefile.sys",
    "usedBytes": 107663360,
    "freeBytes": 2442473472
  }
]

الشبكة

العمليات

Golang تم تحريره بواسطة www.golangdev.cn