Skip to content

Commit

Permalink
inital
Browse files Browse the repository at this point in the history
  • Loading branch information
uselibrary committed Apr 21, 2023
1 parent 0031c2e commit 1b84470
Show file tree
Hide file tree
Showing 33 changed files with 1,847 additions and 0 deletions.
14 changes: 14 additions & 0 deletions data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "demo.domain.com",
"telegram": {
"token": "123456789:ABCD45-VCSIDUIC78VS78RN",
"chat_id": "123456789"
},
"disk": "dev/sda1",
"status": {
"cpu": 0.5,
"diskpercentage": 30,
"network": 500,
"memorypercentage": 50
}
}
Binary file added data/levelDB/000007.ldb
Binary file not shown.
Binary file added data/levelDB/000028.ldb
Binary file not shown.
Binary file added data/levelDB/000047.ldb
Binary file not shown.
Binary file added data/levelDB/000096.ldb
Binary file not shown.
Binary file added data/levelDB/000107.ldb
Binary file not shown.
Binary file added data/levelDB/000131.ldb
Binary file not shown.
Binary file added data/levelDB/000132.ldb
Binary file not shown.
Binary file added data/levelDB/000158.ldb
Binary file not shown.
Binary file added data/levelDB/000159.ldb
Binary file not shown.
Binary file added data/levelDB/000172.ldb
Binary file not shown.
Binary file added data/levelDB/000183.ldb
Binary file not shown.
Binary file added data/levelDB/000209.ldb
Binary file not shown.
Binary file added data/levelDB/000210.ldb
Binary file not shown.
Binary file added data/levelDB/000219.ldb
Binary file not shown.
Binary file added data/levelDB/000230.ldb
Binary file not shown.
Empty file added data/levelDB/000231.log
Empty file.
1 change: 1 addition & 0 deletions data/levelDB/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000232
1 change: 1 addition & 0 deletions data/levelDB/CURRENT.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000229
Empty file added data/levelDB/LOCK
Empty file.
1,278 changes: 1,278 additions & 0 deletions data/levelDB/LOG

Large diffs are not rendered by default.

Binary file added data/levelDB/MANIFEST-000232
Binary file not shown.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module systemMonitor

go 1.20

require github.com/syndtr/goleveldb v1.0.0

require (
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
golang.org/x/sys v0.7.0 // indirect
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
28 changes: 28 additions & 0 deletions src/arguments/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package arguments

import (
"os"
)

// function to get command line arguments
// get command line arguments
func GetCommandArguments() string {
// get command line arguments
arguments := os.Args
// get config file path
configFilePath := ""
// get command line arguments length
argumentsLength := len(arguments)
// get command line arguments
for i := 1; i < argumentsLength; i++ {
// get config file path
if arguments[i] == "--config" || arguments[i] == "-c" {
// get config file path
configFilePath = arguments[i+1]
// break
break
}
}

return configFilePath
}
25 changes: 25 additions & 0 deletions src/collection/cpuUsage/cpuUsage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cpuUsage

import (
"log"
"os/exec"
"strconv"
"strings"
)

// function to get cpu usage
// return: cpu usage in float64
func GetCPUUsage() float64 {
// get cpu usage
out, err := exec.Command("bash", "-c", "top -bn1 | grep load | awk '{printf \"%.2f\", $(NF-2)}'").Output()
if err != nil {
log.Println(err)
return 0
}
cpuUsage, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err != nil {
log.Println(err)
return 0
}
return cpuUsage
}
38 changes: 38 additions & 0 deletions src/collection/diskUsage/diskUsage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package diskUsage

import (
"log"
"os/exec"
"strconv"
"strings"
)

// function to get disk usage
// variable: disk name, such as /dev/vda1 or /dev/sda1
// return: used space, percentage
func GetDiskUsage(disk string) (float64, float64) {
// get disk usage, use df -B M to get the result in MB, and use grep to get the result of the disk
// command: df -B M | grep disk | awk '{print $3, $5}'
command := "df -B M | grep " + disk + " | awk '{print $3, $5}'"
//out, err := exec.Command("bash", "-c", "df -B M | grep /dev/sda1 | awk '{print $3, $5}'").Output()
out, err := exec.Command("bash", "-c", command).Output()
if err != nil {
log.Println(err)
return 0, 0
}
// parse output
split := strings.Split(strings.TrimSpace(string(out)), " ")
usedSpace, err := strconv.ParseFloat(strings.TrimSuffix(split[0], "M"), 64)
if err != nil {
log.Println(err)
return 0, 0
}
// convert to GB, and keep 2 decimal places
usedSpace = float64(int(usedSpace/1024*100)) / 100
usagePercentage, err := strconv.ParseFloat(strings.TrimSuffix(split[1], "%"), 64)
if err != nil {
log.Println(err)
return 0, 0
}
return usedSpace, usagePercentage
}
62 changes: 62 additions & 0 deletions src/collection/networkUsage/networkUsage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package networkUsage

import (
"log"
"os/exec"
"strconv"
"strings"
)

// function to get monthly bandwidth usage via vnstat
// return: total bandwidth usage, float64
func GetMonthlyBandwidthUsage() float64 {

// get monthly bandwidth usage via vnstat
monthlyBandwidthUsage, err := exec.Command("vnstat", "-m", "--oneline").Output()
if err != nil {
log.Fatal(err)
}

// the output of vnstat is like this:
// 1;ens18;2023-04-19;267.21 MiB;719.30 KiB;267.91 MiB;40.27 kbit/s;2023-04;9.57 GiB;1.28 GiB;10.85 GiB;57.87 kbit/s;10.73 GiB;1.32 GiB;12.05 GiB

// split the output by ";", the 11th element is the total bandwidth usage
monthlyBandwidthUsageSplit := strings.Split(string(monthlyBandwidthUsage), ";")

// the total bandwidth usage may end with " KiB", " MiB", " GiB" and " TiB", use the last 3 characters to determine the unit
// then convert the total bandwidth usage to float64
var monthlyBandwidthUsageFloat64 float64
if monthlyBandwidthUsageSplit[10][len(monthlyBandwidthUsageSplit[10])-3:] == "KiB" {
monthlyBandwidthUsageFloat64, err = strconv.ParseFloat(monthlyBandwidthUsageSplit[10][:len(monthlyBandwidthUsageSplit[10])-4], 64)
if err != nil {
log.Fatal(err)
}
// convert KiB to GiB
monthlyBandwidthUsageFloat64 = monthlyBandwidthUsageFloat64 / 1024 / 1024
}
if monthlyBandwidthUsageSplit[10][len(monthlyBandwidthUsageSplit[10])-3:] == "MiB" {
monthlyBandwidthUsageFloat64, err = strconv.ParseFloat(monthlyBandwidthUsageSplit[10][:len(monthlyBandwidthUsageSplit[10])-4], 64)
if err != nil {
log.Fatal(err)
}
monthlyBandwidthUsageFloat64 = monthlyBandwidthUsageFloat64 / 1024
}
if monthlyBandwidthUsageSplit[10][len(monthlyBandwidthUsageSplit[10])-3:] == "GiB" {
monthlyBandwidthUsageFloat64, err = strconv.ParseFloat(monthlyBandwidthUsageSplit[10][:len(monthlyBandwidthUsageSplit[10])-4], 64)
if err != nil {
log.Fatal(err)
}
}
if monthlyBandwidthUsageSplit[10][len(monthlyBandwidthUsageSplit[10])-3:] == "TiB" {
monthlyBandwidthUsageFloat64, err = strconv.ParseFloat(monthlyBandwidthUsageSplit[10][:len(monthlyBandwidthUsageSplit[10])-4], 64)
if err != nil {
log.Fatal(err)
}
monthlyBandwidthUsageFloat64 = monthlyBandwidthUsageFloat64 * 1024
}

// keep 2 decimal places
monthlyBandwidthUsageFloat64 = float64(int(monthlyBandwidthUsageFloat64*100)) / 100

return monthlyBandwidthUsageFloat64
}
36 changes: 36 additions & 0 deletions src/collection/ramUsage/ramUsage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ramUsage

import (
"log"
"os/exec"
"strconv"
"strings"
)

// function to get ram usage
// return: used ram (M) and usage percentage

func GetRamUsage() (float64, float64) {
// get ram usage
out, err := exec.Command("bash", "-c", "free -m | grep Mem | awk '{print $3, $2}'").Output()
if err != nil {
log.Println(err)
return 0, 0
}
// parse output
split := strings.Split(strings.TrimSpace(string(out)), " ")
usedRam, err := strconv.ParseFloat(split[0], 64)
if err != nil {
log.Println(err)
return 0, 0
}
totalRam, err := strconv.ParseFloat(split[1], 64)
if err != nil {
log.Println(err)
return 0, 0
}
usagePercentage := usedRam / totalRam * 100
// keep 2 decimal places
usagePercentage = float64(int(usagePercentage*100)) / 100
return usedRam, usagePercentage
}
92 changes: 92 additions & 0 deletions src/levelDB/levelDB.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package levelDB

import (
"log"
"os"

"github.com/syndtr/goleveldb/leveldb"
)

// function to check if a levelDB database exists
// variable: path to the levelDB database
// return: True if the levelDB database exists, False if not
func CheckLevelDB(path string) bool {
// check if the levelDB database exists
if _, err := os.Stat(path); os.IsNotExist(err) {
// the levelDB database does not exist
return false
} else {
// the levelDB database exists
return true
}
}

// function to create a levelDB database
// variable: path to the levelDB database
// return: none
func CreateLevelDB(path string) {
// create the levelDB database
db, err := leveldb.OpenFile(path, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

// function to get the value of a key in a levelDB database
// variable: path to the levelDB database, key
// return: value of the key
func GetLevelDBValue(path string, key string) string {
// open the levelDB database
db, err := leveldb.OpenFile(path, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// get the value of the key
value, err := db.Get([]byte(key), nil)
if err != nil {
log.Fatal(err)
}

// return the value
return string(value)
}

// function to update the value of a key in a levelDB database
// variable: path to the levelDB database, key, value
// return: none
func UpdateLevelDBValue(path string, key string, value string) {
// open the levelDB database
db, err := leveldb.OpenFile(path, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// update the value of the key
err = db.Put([]byte(key), []byte(value), nil)
if err != nil {
log.Fatal(err)
}
}

// function to write a key-value pair to a levelDB database
// variable: path to the levelDB database, key, value
// key value format: CPU: True, Memory: True, Disk: True, Network: True
// return: none
func WriteLevelDB(path string, key string, value string) {
// open the levelDB database
db, err := leveldb.OpenFile(path, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// write a key-value pair to the levelDB database
err = db.Put([]byte(key), []byte(value), nil)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 1b84470

Please sign in to comment.