Skip to content

Commit

Permalink
add process memory + boot time
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricve committed Feb 15, 2023
1 parent deb0308 commit 3cb3809
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 129 deletions.
43 changes: 31 additions & 12 deletions machinery/src/cloud/Cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func GetSystemInfo() (models.System, error) {
var usedMem uint64 = 0
var totalMem uint64 = 0
var freeMem uint64 = 0

var processUsedMem uint64 = 0

architecture := ""
cpuId := ""
KernelVersion := ""
Expand Down Expand Up @@ -133,18 +136,27 @@ func GetSystemInfo() (models.System, error) {
}
}

process, err := sysinfo.Self()
if err == nil {
memInfo, err := process.Memory()
if err == nil {
processUsedMem = memInfo.Resident
}
}

system := models.System{
Hostname: hostname,
CPUId: cpuId,
KernelVersion: KernelVersion,
Version: agentVersion,
MACs: MACs,
IPs: IPs,
BootTime: uint64(bootTime.Unix()),
Architecture: architecture,
UsedMemory: usedMem,
TotalMemory: totalMem,
FreeMemory: freeMem,
Hostname: hostname,
CPUId: cpuId,
KernelVersion: KernelVersion,
Version: agentVersion,
MACs: MACs,
IPs: IPs,
BootTime: uint64(bootTime.Unix()),
Architecture: architecture,
UsedMemory: usedMem,
TotalMemory: totalMem,
FreeMemory: freeMem,
ProcessUsedMemory: processUsedMem,
}

return system, nil
Expand Down Expand Up @@ -200,6 +212,11 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models.
uptimeString := carbon.Parse(uptimeFormatted).DiffForHumans()
uptimeString = strings.ReplaceAll(uptimeString, "ago", "")

// Do the same for boottime
bootTimeFormatted := time.Unix(int64(system.BootTime), 0).Format("2006-01-02 15:04:05")
boottimeString := carbon.Parse(bootTimeFormatted).DiffForHumans()
boottimeString = strings.ReplaceAll(boottimeString, "ago", "")

// We'll check which mode is enabled for the camera.
onvifEnabled := "false"
if config.Capture.IPCamera.ONVIFXAddr != "" {
Expand Down Expand Up @@ -239,13 +256,15 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models.
"totalMemory" : "%d",
"usedMemory" : "%d",
"freeMemory" : "%d",
"processMemory" : "%d",
"mac_list" : %s,
"ip_list" : %s,
"board" : "",
"disk1size" : "%s",
"disk3size" : "%s",
"diskvdasize" : "%s",
"uptime" : "%s",
"boot_time" : "%s",
"siteID" : "%s",
"onvif" : "%s",
"numberoffiles" : "33",
Expand All @@ -254,7 +273,7 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models.
"docker" : true,
"kios" : false,
"raspberrypi" : false
}`, config.Key, system.Version, system.CPUId, username, key, name, isEnterprise, system.Hostname, system.Architecture, system.TotalMemory, system.UsedMemory, system.FreeMemory, macs, ips, "0", "0", "0", uptimeString, config.HubSite, onvifEnabled)
}`, config.Key, system.Version, system.CPUId, username, key, name, isEnterprise, system.Hostname, system.Architecture, system.TotalMemory, system.UsedMemory, system.FreeMemory, system.ProcessUsedMemory, macs, ips, "0", "0", "0", uptimeString, boottimeString, config.HubSite, onvifEnabled)

var jsonStr = []byte(object)
buffy := bytes.NewBuffer(jsonStr)
Expand Down
25 changes: 13 additions & 12 deletions machinery/src/models/System.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package models

type System struct {
CPUId string `json:"cpu_idle" bson:"cpu_idle"`
Hostname string `json:"hostname" bson:"hostname"`
Version string `json:"version" bson:"version"`
Release string `json:"release" bson:"release"`
BootTime uint64 `json:"boot_time" bson:"boot_time"`
KernelVersion string `json:"kernel_version" bson:"kernel_version"`
MACs []string `json:"macs" bson:"macs"`
IPs []string `json:"ips" bson:"ips"`
Architecture string `json:"architecture" bson:"architecture"`
UsedMemory uint64 `json:"used_memory" bson:"used_memory"`
TotalMemory uint64 `json:"total_memory" bson:"total_memory"`
FreeMemory uint64 `json:"free_memory" bson:"free_memory"`
CPUId string `json:"cpu_idle" bson:"cpu_idle"`
Hostname string `json:"hostname" bson:"hostname"`
Version string `json:"version" bson:"version"`
Release string `json:"release" bson:"release"`
BootTime uint64 `json:"boot_time" bson:"boot_time"`
KernelVersion string `json:"kernel_version" bson:"kernel_version"`
MACs []string `json:"macs" bson:"macs"`
IPs []string `json:"ips" bson:"ips"`
Architecture string `json:"architecture" bson:"architecture"`
UsedMemory uint64 `json:"used_memory" bson:"used_memory"`
TotalMemory uint64 `json:"total_memory" bson:"total_memory"`
FreeMemory uint64 `json:"free_memory" bson:"free_memory"`
ProcessUsedMemory uint64 `json:"process_used_memory" bson:"process_used_memory"`
}
105 changes: 0 additions & 105 deletions machinery/src/utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,108 +271,3 @@ func CreateFragmentedMP4(fullName string, fragmentedDuration int64) {
os.Remove(fullName)
os.Rename(fullName+"f.mp4", fullName)
}

/*func FloatToString(input_num float64) string {
// to convert a float number to a string
return strconv.FormatFloat(input_num, 'f', 6, 64)
}
func ParseFMP4(file *bytes.Reader) (error, uint64, uint64, []*mp4.MediaSegment, uint64) {
var ftypSize, moovSize uint64
var segments []*mp4.MediaSegment
var duration uint64
parsedMp4, err := mp4.DecodeFile(file, mp4.WithDecodeMode(mp4.DecModeLazyMdat))
if parsedMp4 != nil && parsedMp4.Init != nil && err == nil {
ftypSize = parsedMp4.Init.Ftyp.Size()
moovSize = parsedMp4.Init.Moov.Size()
duration = parsedMp4.Moov.Trak.Tkhd.Duration
segments = parsedMp4.Segments
}
return err, ftypSize, moovSize, segments, duration
}
func ParseFMP4Detail(fullName string) {
// open fullName
file, err := os.Open(fullName)
if err != nil {
log.Log.Error("Error opening file: " + err.Error())
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Log.Error("Error reading file: " + err.Error())
}
fileReader := bytes.NewReader(fileBytes)
err, ftypSize, moovSize, segments, dur := ParseFMP4(fileReader)
fmt.Println("========== Fragmented details =================")
fmt.Println(dur)
fmt.Println(ftypSize)
fmt.Println(moovSize)
fmt.Println(len(segments))
// Calculate duration of segments
var totalDuration uint64
for _, segment := range segments {
fragments := segment.Fragments
for _, fragment := range fragments {
var sampleDuration uint32
samples := fragment.Moof.Traf.Trun.Samples
for _, sample := range samples {
sampleDuration += sample.Dur
}
fmt.Println(sampleDuration)
totalDuration += uint64(sampleDuration)
}
}
misingDuration := dur - totalDuration/100
fmt.Println(misingDuration)
fmt.Println(totalDuration/100 + misingDuration)
}
func CreateFragmentByteRanges(log log.Logging, fileName string, ftypSize uint64, moovSize uint64, segments []*mp4.MediaSegment) (string, []models.BytesRangeOnTime) {
size := strconv.FormatInt(int64(ftypSize+moovSize), 10)
var fileFragments strings.Builder
fileFragments.WriteString("#EXT-X-MAP:URI=\"" + fileName + "\",BYTERANGE=\"" + size + "@0\"\n")
var bytesRangeOnTime []models.BytesRangeOnTime
var currentTime uint32
for _, segment := range segments {
fragments := segment.Fragments
for _, fragment := range fragments {
var sampleDuration uint32
samples := fragment.Moof.Traf.Trun.Samples
for _, sample := range samples {
sampleDuration += sample.Dur
}
currentTime = currentTime + sampleDuration
duration := FloatToString(float64(sampleDuration / 100000))
startPos := fragment.Moof.StartPos
start := strconv.FormatInt(int64(startPos), 10)
totalSize := fragment.Mdat.Size() + fragment.Moof.Size()
size := strconv.FormatInt(int64(totalSize), 10)
fileFragments.WriteString("#EXTINF:" + duration + ",\n") // @TODO calculate the duration
fileFragments.WriteString("#EXT-X-BYTERANGE:" + size + "@" + start + "\n")
fileFragments.WriteString(fileName + "\n")
byteRange := models.BytesRangeOnTime{
Duration: duration,
Time: FloatToString(float64(currentTime) / 100000),
Range: size + "@" + start,
}
bytesRangeOnTime = append(bytesRangeOnTime, byteRange)
}
}
bytesRanges := fileFragments.String()
log.Info("Fragments calculate from " + fileName + ": " + bytesRanges)
return bytesRanges, bytesRangeOnTime
}*/

0 comments on commit 3cb3809

Please sign in to comment.