Add total CPU % to /info/system endpoint#2297
Conversation
Signed-off-by: DL6ER <dl6er@dl6er.de>
…rnel, older than 2.6.33 released 2010) Signed-off-by: DL6ER <dl6er@dl6er.de>
| fclose(statfile); | ||
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the variable user is only used if the sscanf call successfully reads all 10 items. This can be achieved by moving the code that uses user within the conditional block that checks the return value of sscanf.
- Move the code that uses
userand other variables populated bysscanfwithin the conditional block that checks the return value ofsscanf. - Ensure that the function returns
falseifsscanffails to read all 10 items, preventing the use of uninitialized variables.
| @@ -335,3 +335,16 @@ | ||
|
|
||
| break; | ||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; | ||
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
|
|
||
| fclose(statfile); | ||
| return true; | ||
| } | ||
| @@ -346,16 +359,3 @@ | ||
| fclose(statfile); | ||
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; | ||
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
|
|
||
| return true; | ||
| return false; | ||
| } |
| fclose(statfile); | ||
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Copilot Autofix
AI over 1 year ago
Copilot could not generate an autofix suggestion
Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Copilot Autofix
AI over 1 year ago
Copilot could not generate an autofix suggestion
Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the variable guest_nice is only used if the sscanf function successfully reads all 10 items. This can be done by adding an explicit check for the return value of sscanf before using guest_nice. If the check fails, the function should return false to prevent further execution with potentially uninitialized variables.
| @@ -325,6 +325,7 @@ | ||
| { | ||
| if(sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| int num_items = sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| &user, &nice, &system, &idle, | ||
| &iowait, &irq, &softirq, &steal, | ||
| &guest, &guest_nice) != 10) | ||
| &guest, &guest_nice); | ||
| if(num_items != 10) | ||
| { |
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the system variable is only used if the sscanf call successfully reads all 10 items. This can be done by adding a check for the return value of sscanf and ensuring that the subsequent code only executes if the return value is 10. This will prevent the use of uninitialized variables and ensure that the code behaves as expected.
| @@ -353,9 +353,16 @@ | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
|
|
||
| return true; | ||
| fclose(statfile); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| log_warn("No CPU line found in /proc/stat"); | ||
| fclose(statfile); | ||
| return false; | ||
| } |
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the sscanf call returns at least 6 items before using the irq variable. This can be done by adding a check to verify that the return value of sscanf is at least 6 before proceeding with the use of the irq variable. If the return value is less than 6, we should log an error and return false.
| @@ -325,6 +325,7 @@ | ||
| { | ||
| if(sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| int num_items = sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| &user, &nice, &system, &idle, | ||
| &iowait, &irq, &softirq, &steal, | ||
| &guest, &guest_nice) != 10) | ||
| &guest, &guest_nice); | ||
| if(num_items < 6) | ||
| { | ||
| @@ -332,2 +333,8 @@ | ||
| fclose(statfile); | ||
| return false; | ||
| } | ||
| if(num_items != 10) | ||
| { | ||
| log_debug(DEBUG_ANY, "Failed to parse all CPU fields in /proc/stat"); | ||
| fclose(statfile); | ||
| return false; |
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the return value of sscanf is checked to confirm that all expected items were successfully read before using the variables. Specifically, we should check that sscanf returns 10, indicating that all 10 items were read. If the return value is not 10, we should handle the error appropriately, such as by logging an error message and returning false.
| @@ -325,6 +325,7 @@ | ||
| { | ||
| if(sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| int num_items = sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| &user, &nice, &system, &idle, | ||
| &iowait, &irq, &softirq, &steal, | ||
| &guest, &guest_nice) != 10) | ||
| &guest, &guest_nice); | ||
| if(num_items != 10) | ||
| { |
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the idle variable is only used if the sscanf call successfully reads all 10 items. This can be done by adding a check for the return value of sscanf and ensuring that the subsequent code that uses idle is only executed if the check passes.
| @@ -335,3 +335,16 @@ | ||
|
|
||
| break; | ||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; | ||
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
|
|
||
| fclose(statfile); | ||
| return true; | ||
| } | ||
| @@ -346,16 +359,3 @@ | ||
| fclose(statfile); | ||
|
|
||
| // Guest time is already accounted in usertime | ||
| user -= guest; | ||
| nice -= guest_nice; | ||
|
|
||
| // Fields existing on kernels >= 2.6 | ||
| // (and RHEL's patched kernel 2.4...) | ||
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; | ||
| *total_sum = busy_sum + *idle_sum; | ||
|
|
||
| return true; | ||
| return false; | ||
| } |
| const unsigned long long int sys_all = system + irq + softirq; | ||
| const unsigned long long int virtual = guest + guest_nice; | ||
| const unsigned long long int busy_sum = user + nice + sys_all + steal + virtual; | ||
| *idle_sum = idle + iowait; |
Check failure
Code scanning / CodeQL
Missing return-value check for a 'scanf'-like function
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to ensure that the sscanf call successfully reads all 10 items before using the variables populated by sscanf. This can be done by adding a check for the return value of sscanf and ensuring that it returns exactly 10. If the return value is not 10, we should handle the error appropriately and avoid using the uninitialized variables.
| @@ -325,6 +325,7 @@ | ||
| { | ||
| if(sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| int num_items = sscanf(line, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | ||
| &user, &nice, &system, &idle, | ||
| &iowait, &irq, &softirq, &steal, | ||
| &guest, &guest_nice) != 10) | ||
| &guest, &guest_nice); | ||
| if(num_items != 10) | ||
| { |
Signed-off-by: DL6ER <dl6er@dl6er.de>
|
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
Signed-off-by: DL6ER <dl6er@dl6er.de>
|
Conflicts have been resolved. |
|
I removed the last commit that changed the CPU% from FTL to total in the PADD callback. |
|
This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there: https://discourse.pi-hole.net/t/reimplement-display-of-cpu-temperature-and-load-averages/77390/4 |
What does this implement/fix?
Calculate and expose total CPU % for the web interface, see related PR.
Related issue or feature (if applicable): Related to pi-hole/web#3261
Pull request in docs with documentation (if applicable): N/A
By submitting this pull request, I confirm the following:
git rebase)Checklist:
developmentalbranch.