From 8ecead97ee9bda4efa383937cf2e57b7f49b8aad Mon Sep 17 00:00:00 2001 From: serxoz Date: Tue, 30 May 2023 10:01:34 +0200 Subject: [PATCH] primeiro --- battery | 30 +++++++++ cpu-usage.c | 154 +++++++++++++++++++++++++++++++++++++++++++++ ram.c | 59 +++++++++++++++++ status.sh | 15 +++++ temperature-laptop | 3 + weather | 5 ++ 6 files changed, 266 insertions(+) create mode 100755 battery create mode 100644 cpu-usage.c create mode 100644 ram.c create mode 100755 status.sh create mode 100755 temperature-laptop create mode 100755 weather diff --git a/battery b/battery new file mode 100755 index 0000000..9659f19 --- /dev/null +++ b/battery @@ -0,0 +1,30 @@ +#!/bin/sh + +STATUS=$(sysctl -nh hw.acpi.battery.state) +LOAD=$(sysctl -nh hw.acpi.battery.life) + +if [ $STATUS = 2 ] +then + # cargando + echo  $LOAD% +elif [ $STATUS = 1 ] +then + #descargando + if [ $LOAD -lt 6 ] + then + echo  $LOAD% + elif [ $LOAD -lt 26 ] + then + echo  $LOAD% + elif [ $LOAD -lt 56 ] + then + echo  $LOAD% + elif [ $LOAD -lt 81 ] + then + echo  $LOAD% + else + echo  $LOAD% + fi +else + echo  $LOAD% +fi diff --git a/cpu-usage.c b/cpu-usage.c new file mode 100644 index 0000000..344945b --- /dev/null +++ b/cpu-usage.c @@ -0,0 +1,154 @@ +// 181110 created +// 200603 rewritten sh version to C to use less CPU when used in tint2 taskbar +// - in sync with information shown in top(1) +// - uses sysctl kern.cp_time (one statistics for all CPU cores) +// - utilization must be calculated by hand every N time slices - TMP file used for storing old values between calculations +// - this program is called from tint2 panel every 1 second +#include +#include +#include +#include // strtol() +#include +#include +#include +#include +#include // sleep() + +#define DEBUG 0 +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_RESET "\x1b[0m" +#define dprintf(fmt, ...) \ +do { if (DEBUG) printf(ANSI_COLOR_YELLOW "DBG INFO %s:%d %s(): " \ + ANSI_COLOR_RESET fmt, __FILE__, __LINE__, __func__,\ + ##__VA_ARGS__); } while (0) + +/* + * kern.cp_time + * Aggregated CPU state counters for + * - user + * - nice + * - system + * - interrupt + * - idle + * kern.cp_times - as kern.cp_time but for each core + * + * CPU utilization = user + nice + system + interrupt + */ +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 +#define TMP_FILE "/tmp/cpu_usage" + +bool file_exists(void); +void write_old(long str[]); +void read_file(void); + +long cur[CPUSTATES]; +long last[CPUSTATES] = {}; + +bool file_exists(void) +{ + if (access(TMP_FILE, F_OK) != -1) + { + return 1; + } + else + { + return 0; + } +} + +void read_file(void) +{ + dprintf("Reading previous state from file\n"); + + FILE *fp; + fp = fopen(TMP_FILE, "r"); + + for (int state = 0; state calculated utilisation: %.1f cur[CP_IDLE]: %ld last[CP_IDLE]: %ld\n", utilization, cur[CP_IDLE], last[CP_IDLE]); + return utilization; + } + else + { + printf("should create new file\n"); + write_old(cur); + return 0.0; // return 0%, will be calculated on next iteration + } + + return utilization; +} + +#ifndef NO_MAIN +int main() +{ + double utilization = get_cpu_utilization(); + printf("龍%.1f%%\n", utilization); + + return 0; +} +#endif // NO_MAIN diff --git a/ram.c b/ram.c new file mode 100644 index 0000000..fbb8cd9 --- /dev/null +++ b/ram.c @@ -0,0 +1,59 @@ +// 181227 created sh version +// 200601 rewritten in C to use less CPU when used in tint2 taskbar +#include +#include +#include // exit() + +#include +#include +#include +#include + +int64_t get_sysctl(int mib1, int mib2) +{ + int mib[2]; + int64_t value; + + mib[0] = mib1; + mib[1] = mib2; + + size_t len = sizeof(value); + if (sysctl(mib, 2, &value, &len, NULL, 0) < 0) + { + printf("error, mib[%d, %d]\n", mib[0], mib[1]); + exit(-1); + } + + return value; +} + +int main() +{ + int64_t phy_mem = get_sysctl(CTL_HW, HW_PHYSMEM); + int page_size = get_sysctl(CTL_HW, HW_PAGESIZE); + + struct vmtotal vm_info; + int mib[2]; + mib[0] = CTL_VM; + mib[1] = VM_TOTAL; + size_t len = sizeof(vm_info); + if (sysctl(mib, 2, &vm_info, &len, NULL, 0) == -1) + { + printf("error on line: %d\n", __LINE__); + return(-2); + } + + /* uint16_t phy_mb = phy_mem/1024/1024; */ + /* uint16_t vm_free_mb = vm_info.t_free * page_size / 1024 / 1024; */ + float phy_gb = phy_mem/1024.0/1024.0/1024.0; + float vm_free_gb = vm_info.t_free * page_size / 1024.0 / 1024.0 / 1024.0; + /* uint8_t free_percent = (vm_free_mb * 100 / phy_mb); */ + + /* printf("RAM: %d%%\n", free_percent); */ + /* printf("%d GB\n", vm_free_mb); */ + /* printf("%d GB\n", phy_mb); */ + printf(" %.1fG/%.1fG\n", phy_gb-vm_free_gb, phy_gb); + + + return 0; +} diff --git a/status.sh b/status.sh new file mode 100755 index 0000000..4e75138 --- /dev/null +++ b/status.sh @@ -0,0 +1,15 @@ +#!/usr/local/bin/bash + + +while true +do + # WEATHER=$(/home/serxoz/bin/statusbar/weather) + RAM=$(/home/serxoz/bin/statusbar/ram) + CPU=$(/home/serxoz/bin/statusbar/cpu) + TEMP=$(/home/serxoz/bin/statusbar/temperature-laptop) + BAT=$(/home/serxoz/bin/statusbar/battery) + DATE=$(date '+ %d/%m/%Y  %H:%M%p') + + xsetroot -name "$WEATHER $RAM $CPU $TEMP $BAT $DATE" + sleep 2 +done diff --git a/temperature-laptop b/temperature-laptop new file mode 100755 index 0000000..0cf8c06 --- /dev/null +++ b/temperature-laptop @@ -0,0 +1,3 @@ +#!/bin/sh +T=$(sysctl -a | grep temperature | cut -d ' ' -f2 | cut -d 'C' -f1) +echo  $T°C diff --git a/weather b/weather new file mode 100755 index 0000000..a75949e --- /dev/null +++ b/weather @@ -0,0 +1,5 @@ +#!/bin/sh + +# curl "https://wttr.in/Viveiro?format=3" 2>/dev/null | cut -d\: -f2 | sed 's/ / /g' +#curl "https://wttr.in/Viveiro?format=3" 2>/dev/null | cut -d\: -f2 | sed 's/ //g' +echo `curl 'https://wttr.in/Viveiro?format=%c%t+%m+%w' | grep -v "Unknown location" 2>/dev/null`