From b0235c0f88e451d7671e252ac823239db6b2a561 Mon Sep 17 00:00:00 2001 From: "Kreato (Work)" Date: Fri, 23 Aug 2024 10:10:47 +0300 Subject: [PATCH] osHealth: reduce float depth on places that don't need it --- common/logger.go | 11 +++++++++-- osHealth/disk.go | 14 +++++++------- osHealth/ram.go | 8 ++++---- osHealth/sysload.go | 4 ++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/common/logger.go b/common/logger.go index 073aff3..4a66468 100644 --- a/common/logger.go +++ b/common/logger.go @@ -26,8 +26,9 @@ func LogError(err string) { logrus.Error(err) } -func PrettyPrint(name string, lessOrMore string, value float64, hasPercentage bool) { +func PrettyPrint(name string, lessOrMore string, value float64, hasPercentage bool, wantFloat bool) { var par string + var floatDepth int if hasPercentage { par = "%)" @@ -35,5 +36,11 @@ func PrettyPrint(name string, lessOrMore string, value float64, hasPercentage bo par = ")" } - fmt.Println(Blue + name + Reset + " is " + lessOrMore + " (" + strconv.FormatFloat(value, 'f', 2, 64) + par + Reset) + if wantFloat { + floatDepth = 2 + } else { + floatDepth = 0 + } + + fmt.Println(Blue + name + Reset + " is " + lessOrMore + " (" + strconv.FormatFloat(value, 'f', floatDepth, 64) + par + Reset) } diff --git a/osHealth/disk.go b/osHealth/disk.go index dfecaaf..8109b00 100644 --- a/osHealth/disk.go +++ b/osHealth/disk.go @@ -30,10 +30,10 @@ func DiskUsage() { usage, _ := disk.Usage(partition.Mountpoint) if usage.UsedPercent > OsHealthConfig.Part_use_limit { - common.PrettyPrint("Disk usage at " + partition.Mountpoint, common.Fail + " more than " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', -1, 64) + "%", usage.UsedPercent, true) - exceededParts = append(exceededParts, []string{strconv.FormatFloat(usage.UsedPercent, 'f', 2, 64), common.ConvertBytes(usage.Used), common.ConvertBytes(usage.Total), partition.Device, partition.Mountpoint}) + common.PrettyPrint("Disk usage at " + partition.Mountpoint, common.Fail + " more than " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64) + "%", usage.UsedPercent, true, false) + exceededParts = append(exceededParts, []string{strconv.FormatFloat(usage.UsedPercent, 'f', 0, 64), common.ConvertBytes(usage.Used), common.ConvertBytes(usage.Total), partition.Device, partition.Mountpoint}) } else { - common.PrettyPrint("Disk usage at " + partition.Mountpoint, common.Green + " less than " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', -1, 64) + "%", usage.UsedPercent, true) + common.PrettyPrint("Disk usage at " + partition.Mountpoint, common.Green + " less than " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64) + "%", usage.UsedPercent, true, false) } } @@ -45,7 +45,7 @@ func DiskUsage() { table.SetCenterSeparator("|") table.AppendBulk(exceededParts) table.Render() - msg := "Partition usage level has exceeded to " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 2, 64) + "% " + "for the following partitions;\n\n" + output.String() + msg := "Partition usage level has exceeded to " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64) + "% " + "for the following partitions;\n\n" + output.String() msg = strings.Replace(msg, "\n", `\n`, -1) // Check if file exists @@ -66,10 +66,10 @@ func DiskUsage() { common.AlarmCheckDown("disk", msg) - common.RedmineCreate("disk", common.Config.Identifier + " - Diskteki bir (ya da birden fazla) bölümün doluluk seviyesi %"+strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 2, 64)+" üstüne çıktı", output.String()) + common.RedmineCreate("disk", common.Config.Identifier + " - Diskteki bir (ya da birden fazla) bölümün doluluk seviyesi %"+strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64)+" üstüne çıktı", output.String()) } else { - common.AlarmCheckUp("disk", "All partitions are now under the limit of " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 2, 64) + "%") - common.RedmineClose("disk", common.Config.Identifier + " - Bütün disk bölümleri "+strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 2, 64)+"% altına indi, kapatılıyor.") + common.AlarmCheckUp("disk", "All partitions are now under the limit of " + strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64) + "%") + common.RedmineClose("disk", common.Config.Identifier + " - Bütün disk bölümleri "+strconv.FormatFloat(OsHealthConfig.Part_use_limit, 'f', 0, 64)+"% altına indi, kapatılıyor.") } } diff --git a/osHealth/ram.go b/osHealth/ram.go index a0af361..46811ba 100644 --- a/osHealth/ram.go +++ b/osHealth/ram.go @@ -18,11 +18,11 @@ func RamUsage() { ramLimit := OsHealthConfig.Ram_Limit if virtualMemory.UsedPercent > ramLimit { - common.PrettyPrint("RAM Usage", common.Fail + " more than " + strconv.FormatFloat(ramLimit, 'f', 2, 64) + "%", virtualMemory.UsedPercent, true) - common.AlarmCheckDown("ram", "RAM usage limit has exceeded " + strconv.FormatFloat(ramLimit, 'f', 2, 64) + "% (Current: " + strconv.FormatFloat(virtualMemory.UsedPercent, 'f', 2, 64) + "%)") + common.PrettyPrint("RAM Usage", common.Fail + " more than " + strconv.FormatFloat(ramLimit, 'f', 0, 64) + "%", virtualMemory.UsedPercent, true, false) + common.AlarmCheckDown("ram", "RAM usage limit has exceeded " + strconv.FormatFloat(ramLimit, 'f', 0, 64) + "% (Current: " + strconv.FormatFloat(virtualMemory.UsedPercent, 'f', 0, 64) + "%)") } else { - common.PrettyPrint("RAM Usage", common.Green + " less than " + strconv.FormatFloat(ramLimit, 'f', 2, 64) + "%", virtualMemory.UsedPercent, true) - common.AlarmCheckUp("ram", "RAM usage went below " + strconv.FormatFloat(ramLimit, 'f', 2, 64) + "% (Current: " + strconv.FormatFloat(virtualMemory.UsedPercent, 'f', 2, 64) + "%)") + common.PrettyPrint("RAM Usage", common.Green + " less than " + strconv.FormatFloat(ramLimit, 'f', 0, 64) + "%", virtualMemory.UsedPercent, true, false) + common.AlarmCheckUp("ram", "RAM usage went below " + strconv.FormatFloat(ramLimit, 'f', 0, 64) + "% (Current: " + strconv.FormatFloat(virtualMemory.UsedPercent, 'f', 0, 64) + "%)") } } diff --git a/osHealth/sysload.go b/osHealth/sysload.go index cd5ee7a..852c53d 100644 --- a/osHealth/sysload.go +++ b/osHealth/sysload.go @@ -25,8 +25,8 @@ func SysLoad() { } if loadAvg.Load1 > loadLimit { - common.PrettyPrint("System Load", common.Fail + " more than " + strconv.FormatFloat(loadLimit, 'f', 2, 64) + "%", loadAvg.Load1, false) + common.PrettyPrint("System Load", common.Fail + " more than " + strconv.FormatFloat(loadLimit, 'f', 2, 64) + "%", loadAvg.Load1, false, true) } else { - common.PrettyPrint("System Load", common.Green + " less than " + strconv.FormatFloat(loadLimit, 'f', 2, 64) + "%", loadAvg.Load1, false) + common.PrettyPrint("System Load", common.Green + " less than " + strconv.FormatFloat(loadLimit, 'f', 2, 64) + "%", loadAvg.Load1, false, true) } }