System-status/read is throwing "PHP Notice: A non well formed numeric value encountered in /usr/libexec/nethserver/api/system-status/read"

The Error appears on my machine simply by opening the Cockpit Dashboard.
It get’s cought in the getSystemSummary function, so the system information never appears.

PHP says that the error appears four times, on the lines 39, 40, 41 and 42.

The content of the status JSON is:

{
  "status": {
    "memory": {
      "MemTotal": "7958500",
      "MemFree": "200136",
      "MemAvailable": "5082732",
      "Buffers": "20632",
      "Cached": "4779032",
      "SwapCached": "39556",
      "Active": "1143584",
      "Inactive": "5687300",
      "Active(anon)": "1007268",
      "Inactive(anon)": "1142920",
      "Active(file)": "136316",
      "Inactive(file)": "4544380",
      "Unevictable": "0",
      "Mlocked": "0",
      "SwapTotal": "8126460",
      "SwapFree": "7085564",
      "Dirty": "17828",
      "Writeback": "60",
      "AnonPages": "2008144",
      "Mapped": "144372",
      "Shmem": "118640",
      "Slab": "579852",
      "SReclaimable": "505680",
      "SUnreclaim": "74172",
      "KernelStack": "7664",
      "PageTables": "94008",
      "NFS_Unstable": "0",
      "Bounce": "0",
      "WritebackTmp": "0",
      "CommitLimit": "12105708",
      "Committed_AS": "7653376",
      "VmallocTotal": "34359738367",
      "VmallocUsed": "297676",
      "VmallocChunk": "34358947836",
      "HardwareCorrupted": "0",
      "AnonHugePages": "100352",
      "CmaTotal": "0",
      "CmaFree": "0",
      "HugePages_Total": "0",
      "HugePages_Free": "0",
      "HugePages_Rsvd": "0",
      "HugePages_Surp": "0",
      "Hugepagesize": "2048",
      "DirectMap4k": "399432",
      "DirectMap2M": "7938048",
      "DirectMap1G": "0"
    },
    "uptime": {
      "days": 38,
      "hours": 7,
      "minutes": 14,
      "seconds": 23
    },
    "cpu": {
      "model": "AMD E-350 Processor",
      "n": 2
    },
    "kernel": "3.10.0-957.1.3.el7.x86_64",
    "hardware": "To Be Filled By O.E.M. To Be Filled By O.E.M.",
    "release": "NethServer release 7.6.1810 (final)",
    "load": [
      9.87,
      7.6,
      7.23
    ]
  },
  "configuration": ""
}

I can’t reproduce this bug. Have you already tried to restart cockpit service and clean browser cache?
Have you got any particular module installed?

Just restarted and cleared cache, problem persists.
I suppose it could have something to do with php-versions since i have php scl installed (https://wiki.nethserver.org/doku.php?id=php-scl)

I have a /etc/profile.d/enablephp71.sh with the following content:

#!/bin/bash
source scl_source enable php71

Maybe this is interfering…?

Yes try to set the default PHP version.

i reset everything back to the default php version (that is 5.6) and rebooted.
Sadly the problem still persists…

I still can’t reproduce.

Could you please post the output of the following commands?

  • php -v
  • php /usr/libexec/nethserver/api/system-status/read
  • php -r '$data = file_get_contents("/proc/uptime"); print $data; print floor($data/60/60/24)."\n";'
1 Like

I changed now to php 7.1.19 from epel-repo, problem persists. The outputs are:

php -v

PHP 7.1.19 (cli) (built: Jun 21 2018 07:18:00) ( NTS )                                                                                                                              
Copyright (c) 1997-2018 The PHP Group                                                                                                                                               
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

php /usr/libexec/nethserver/api/system-status/read

PHP Notice:  A non well formed numeric value encountered in /usr/libexec/nethserver/api/system-status/read on line 39                                                               
PHP Notice:  A non well formed numeric value encountered in /usr/libexec/nethserver/api/system-status/read on line 40                                                               
PHP Notice:  A non well formed numeric value encountered in /usr/libexec/nethserver/api/system-status/read on line 41                                                               
PHP Notice:  A non well formed numeric value encountered in /usr/libexec/nethserver/api/system-status/read on line 42                                                               
{"status":{"memory":{"MemTotal":"7958500","MemFree":"4774460","MemAvailable":"6190060","Buffers":"20700","Cached":"1189392","SwapCached":"0","Active":"1726988","Inactive":"475628",
"Active(anon)":"1000660","Inactive(anon)":"63388","Active(file)":"726328","Inactive(file)":"412240","Unevictable":"0","Mlocked":"0","SwapTotal":"8126460","SwapFree":"8126460","Dirt
y":"736","Writeback":"0","AnonPages":"992572","Mapped":"172044","Shmem":"71536","Slab":"645360","SReclaimable":"580812","SUnreclaim":"64548","KernelStack":"5504","PageTables":"5761
6","NFS_Unstable":"0","Bounce":"0","WritebackTmp":"0","CommitLimit":"12105708","Committed_AS":"4923528","VmallocTotal":"34359738367","VmallocUsed":"297704","VmallocChunk":"34358947
836","HardwareCorrupted":"0","AnonHugePages":"641024","CmaTotal":"0","CmaFree":"0","HugePages_Total":"0","HugePages_Free":"0","HugePages_Rsvd":"0","HugePages_Surp":"0","Hugepagesiz
e":"2048","DirectMap4k":"225352","DirectMap2M":"4966400","DirectMap1G":"3145728"},"uptime":{"days":0,"hours":12,"minutes":42,"seconds":2},"cpu":{"model":"AMD E-350 Processor","n":2
},"kernel":"3.10.0-957.5.1.el7.x86_64","hardware":"To Be Filled By O.E.M. To Be Filled By O.E.M.","release":"NethServer release 7.6.1810 (final)","load":[0.06,0.39,0.43]},"configur
ation":""}

php -r ‘$data = file_get_contents("/proc/uptime"); print $data; print floor($data/60/60/24)."\n";’

45796.77 81169.27                                                                                                                                                                   
PHP Notice:  A non well formed numeric value encountered in Command line code on line 1                                                                                             
0

Thanks for helping!

Can you try remove and reinstall cockpit?

It’s strange. I can’t reproduce it.

I think it’s because $data is returned as string.
The original code:

function readUptime() {
    $data = file_get_contents('/proc/uptime');
    $upsecs = (int)substr($data, 0, strpos($data, ' '));
    $uptime = array (
        'days' => floor($data/60/60/24),
        'hours' => $data/60/60%24,
        'minutes' => $data/60%60,
        'seconds' => $data%60
    );
    return $uptime;
}

A possible fix (consider if there are better options):

function readUptime() {
    $data = file_get_contents('/proc/uptime');
    $upsecs = (int)substr($data, 0, strpos($data, ' '));
    $uptime = array (
        'days' => floor((float)$data/60/60/24),
        'hours' => (float)$data/60/60%24,
        'minutes' => (float)$data/60%60,
        'seconds' => (float)$data%60
    );
    return $uptime;
}
1 Like

reinstalled cockpit, problem persists.

I think we are near the solution now.
/proc/uptime contains two numbers, so every calculation is not possible, also not with converting to a number before. We’d need to cut out the number we want (which would be the first I think) and then perform all the calculations.

I just don’t get, why you guys don’t get this error, pls post the output of cat /proc/uptime.

If it’s of help here’s an online tester (but with some functions blocked for security):

I just took a look at this code, it seems pretty flawed, pls correct me if I’m wrong, I’m not a php guy:

function readUptime() {
    $data = file_get_contents('/proc/uptime');
    $upsecs = (int)substr($data, 0, strpos($data, ' '));
    $uptime = array (
        'days' => floor($data/60/60/24),
        'hours' => $data/60/60%24,
        'minutes' => $data/60%60,
        'seconds' => $data%60
    );
    return $uptime;
}

Somebody tried to adress the “two numbers” issue already by creating $upsecs, but it is not used. Instead $data is used, looks like a merge issue…

Will test locally if it works when I replace $data with $upsecs.

2 Likes

Works, that was the issue!

I have added the bugfix to the project with this pull request.

In addition: Please look at your local installations, what is the content of that file? Why don’t you guys get that issue?

Ah, I’ve missed that point, I think you’re right. Thanks for the PR.

probably due to a different php warning level or setting

but the uptime shouldn’t be visible on the dashboard still…

Thanks for the PR, for sure it was a mistake by my side when I ported the code from the old web interface.

The standard error should be ignored by the UI, this is why nobody found the bug.
I don’t why on you’re machine the output was redirected to stdout. Maybe a different PHP configuration?

Anyway, thanks for the fix, it has been already merged!

No problem, thanks for accepting the PR.

I wonder why nobody saw the uptime value though. However, now it’s fixed, on to the next :wink:

1 Like