Skip to content

Commit 34f9495

Browse files
authored
Merge pull request #1 from codescalersinternships/development
Psutils-golang
2 parents c44cbc0 + ecdf06f commit 34f9495

File tree

11 files changed

+694
-1
lines changed

11 files changed

+694
-1
lines changed

.github/workflows/main.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Go CI
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
golangci:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-go@v5
12+
with:
13+
go-version: stable
14+
- name: golangci-lint
15+
uses: golangci/golangci-lint-action@v6
16+
with:
17+
version: v1.60
18+
- name: Go Format
19+
uses: Jerome1337/gofmt-action@v1.0.5
20+
with:
21+
gofmt-path: './src'
22+
gofmt-flags: '-l -d'
23+
24+
test:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
- uses: actions/setup-go@v5
29+
with:
30+
go-version: stable
31+
- name: test
32+
run: go test -v ./pkg
33+
34+

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
1-
# psutil-golang-RawanMostafa
1+
# Psutils
2+
3+
This repository implements a human-friendly lib for querying processes, memory info and cpu info
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
10+
11+
## Installation
12+
13+
Get the package
14+
15+
```bash
16+
go get github.com/codescalersinternships/psutil-golang-RawanMostafa
17+
```
18+
Import the package
19+
20+
```go
21+
import github.com/codescalersinternships/psutil-golang-RawanMostafa/pkg
22+
```
23+
24+
## Usage
25+
26+
The psutils package offers different APIs like:
27+
28+
### 1. CPU related APIs
29+
30+
```go
31+
cpuInfo, err := psutils.GetCpuInfo()
32+
if err != nil {
33+
//handle the error
34+
}
35+
```
36+
37+
### 2. Memory related APIs
38+
39+
```go
40+
memInfo, err := psutils.GetMemInfo()
41+
if err != nil {
42+
//handle the error
43+
}
44+
```
45+
### 3. Processes related APIs
46+
47+
1. ```go
48+
procs, err := psutils.GetProcessList()
49+
if err != nil {
50+
//handle the error
51+
}
52+
```
53+
54+
2. ```go
55+
procDetails, err := psutils.GetProcessDetails(1)
56+
if err != nil {
57+
//handle the error
58+
}
59+
```

cmd/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
psutils "github.com/codescalersinternships/psutil-golang-RawanMostafa/pkg"
7+
)
8+
9+
func main() {
10+
cpuInfo, _ := psutils.GetCpuInfo()
11+
log.Printf("%v", cpuInfo)
12+
memInfo, err := psutils.GetMemInfo()
13+
log.Printf("%v", memInfo)
14+
log.Printf("err %v", err)
15+
procs, err := psutils.GetProcessList()
16+
log.Printf("%v\n", procs)
17+
log.Printf("err %v", err)
18+
det, err := psutils.GetProcessDetails(1)
19+
log.Printf("%v\n", det)
20+
log.Printf("err %v", err)
21+
22+
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/codescalersinternships/psutil-golang-RawanMostafa
2+
3+
go 1.23.1
4+
5+
require github.com/stretchr/testify v1.9.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/cpuInfo.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// psutils package implements a human-friendly lib for querying processes, memory info and cpu info
2+
package psutils
3+
4+
import (
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type Loader interface {
11+
load(filePath string) (string, error)
12+
}
13+
14+
type realCpuLoader struct{}
15+
16+
func (l *realCpuLoader) load(filePath string) (string, error) {
17+
return loadFile(filePath)
18+
}
19+
20+
// CpuInfo holds some details about the CPU like number of cores, vendorID, model name, cache size, average CPU frequency
21+
type CpuInfo struct {
22+
NumCores int
23+
VendorId string
24+
ModelName string
25+
CacheSize string
26+
CpuMHZ float32
27+
}
28+
29+
func loadFile(filepath string) (fileData string, err error) {
30+
file, err := os.ReadFile(filepath)
31+
fileData = string(file)
32+
33+
return
34+
}
35+
36+
func setCpuInfo(cpuData string) (cpuInfo CpuInfo, err error) {
37+
var processorsNum int
38+
var totalFreq float32
39+
lines := strings.SplitN(cpuData, "\n", -1)
40+
for _, line := range lines {
41+
if strings.HasPrefix(line, "processor") {
42+
processorsNum++
43+
} else if strings.HasPrefix(line, "vendor_id") {
44+
parts := strings.Split(line, ":")
45+
cpuInfo.VendorId = strings.TrimSpace(parts[1])
46+
47+
} else if strings.HasPrefix(line, "model name") {
48+
parts := strings.Split(line, ":")
49+
cpuInfo.ModelName = strings.TrimSpace(parts[1])
50+
51+
} else if strings.HasPrefix(line, "cache size") {
52+
parts := strings.Split(line, ":")
53+
cpuInfo.CacheSize = strings.TrimSpace(parts[1])
54+
55+
} else if strings.HasPrefix(line, "cpu MHz") {
56+
parts := strings.Split(line, ":")
57+
freq, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 32)
58+
59+
if err != nil {
60+
return cpuInfo, err
61+
}
62+
totalFreq += float32(freq)
63+
}
64+
}
65+
cpuInfo.NumCores = processorsNum
66+
cpuInfo.CpuMHZ = totalFreq / float32(cpuInfo.NumCores)
67+
return
68+
}
69+
70+
// GetCpuInfo returns a CpuInfo type about the CPU in this moment
71+
func GetCpuInfo() (cpuInfo CpuInfo, err error) {
72+
var _ Loader = (*realCpuLoader)(nil)
73+
return getCpuInfo(&realCpuLoader{})
74+
}
75+
76+
func getCpuInfo(loader Loader) (cpuInfo CpuInfo, err error) {
77+
78+
cpuData, err := loader.load("/proc/cpuinfo")
79+
if err != nil {
80+
return
81+
}
82+
cpuInfo, err = setCpuInfo(cpuData)
83+
return
84+
}

0 commit comments

Comments
 (0)