100% CPU after starting metern

Talks about metering
mstuetz
Posts: 94
Joined: Sun Dec 29, 2013 1:20 pm

100% CPU after starting metern

Post by mstuetz » Fri Mar 22, 2019 11:30 am

Hi!
I just installed latest version of metern newly on RPi3 and get the problem, that CPU will be on 100% for php as soon as I start meterN.

Code: Select all

[Fri Mar 22 11:23:24.607925 2019] [:error] [pid 7057] [client 192.168.123.91:56807] PHP Warning:  unlink(/dev/shm/mN_LIVEMEMORY.json): No such file or directory in /var/www/html/metern/admin/admin.php on line 152, referer: http://192.168.123.92/metern/admin/admin.php?startstop=done
[Fri Mar 22 11:23:24.608176 2019] [:error] [pid 7057] [client 192.168.123.91:56807] PHP Warning:  unlink(/dev/shm/mN_ILIVEMEMORY.json): No such file or directory in /var/www/html/metern/admin/admin.php on line 153, referer: http://192.168.123.92/metern/admin/admin.php?startstop=done
[Fri Mar 22 11:23:42.341709 2019] [:error] [pid 7348] [client 192.168.123.91:56808] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1648107192 bytes) in /var/www/html/metern/admin/help.php on line 134, referer: http://192.168.123.92/metern/admin/admin.php?startstop=done
^
I added a sensor with ID 2 and deleted it (with decommission), then I added it again.
If I open "Help and debugger" it comes up with that fault message:

Code: Select all

[Fri Mar 22 11:29:30.349333 2019] [:error] [pid 7377] [client 192.168.123.91:56830] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1648107192 bytes) in /var/www/html/metern/admin/help.php on line 134, referer: http://192.168.123.92/metern/admin/admin.php?startstop=done
Any ideas how I could solve that?
BTW: 123solar runs perfecly smooth on same machine

jeanmarc
Posts: 1822
Joined: Thu Aug 29, 2013 7:16 am

Re: 100% CPU after starting metern

Post by jeanmarc » Fri Mar 22, 2019 3:00 pm

Hi,
Sure mN/123s try to get live datas as fast as possible. That could be really intensive, usually it's mostly the com apps that reduce the charge.
Let's say it reads continuously a SHM, it will eat your CPU. You can reduce the requests via Main config in admin "Script delay".
Cheers

jeanmarc
Posts: 1822
Joined: Thu Aug 29, 2013 7:16 am

Re: 100% CPU after starting metern

Post by jeanmarc » Fri Mar 22, 2019 3:42 pm

Did you start and stop mN ? After the reassignement ? ;)

mstuetz
Posts: 94
Joined: Sun Dec 29, 2013 1:20 pm

Re: 100% CPU after starting metern

Post by mstuetz » Fri Mar 22, 2019 5:38 pm

yes, off course. I even removed whole config and readded it again. Start-Stop as well a couple of times.
Script delay is at 5000ms already, but does not matter. Its not a script issue (command is somply "awk ... textfile").
I guess I have to remove whole metern and startover again...

jeanmarc
Posts: 1822
Joined: Thu Aug 29, 2013 7:16 am

Re: 100% CPU after starting metern

Post by jeanmarc » Fri Mar 22, 2019 5:47 pm

You rather avoid sending direct commands, you need a com app that does "awk whatever".
Is the com test work ?

mstuetz
Posts: 94
Joined: Sun Dec 29, 2013 1:20 pm

Re: 100% CPU after starting metern

Post by mstuetz » Fri Mar 22, 2019 6:01 pm

Yes, thats what I did (xxx.sh as meter command that does the awk).
Anyhow, after reinstall it seems better so far. I guess there might be a little problem when "decomissioning" a meter/sensor. I got a couple of fault messages when I tried that.

Code: Select all

[Fri Mar 22 11:12:48.537701 2019] [:error] [pid 9976] [client 192.168.123.92:41382] PHP Notice:  Undefined offset: 2 in /var/www/html/metern/admin/decommissioning2.php on line 96, referer: http://127.0.0.1:9898/metern/admin/decommissioning1.php
[Fri Mar 22 11:12:48.538070 2019] [:error] [pid 9976] [client 192.168.123.92:41382] PHP Notice:  Undefined offset: 2 in /var/www/html/metern/admin/decommissioning2.php on line 96, referer: http://127.0.0.1:9898/metern/admin/decommissioning1.php
[Fri Mar 22 11:12:48.538246 2019] [:error] [pid 9976] [client 192.168.123.92:41382] PHP Notice:  Undefined offset: 2 in /var/www/html/metern/admin/decommissioning2.php on line 96, referer: http://127.0.0.1:9898/metern/admin/decommissioning1.php
[Fri Mar 22 11:12:48.538394 2019] [:error] [pid 9976] [client 192.168.123.92:41382] PHP Notice:  Undefined offset: 2 in /var/www/html/metern/admin/decommissioning2.php on line 96, referer: http://127.0.0.1:9898/metern/admin/decommissioning1.php
[Fri Mar 22 11:13:10.066955 2019] [:error] [pid 7348] [client 192.168.123.92:41384] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1363047352 bytes) in /var/www/html/metern/admin/help.php on line 134, referer: http://127.0.0.1:9898/metern/admin/admin.php?startstop=done

jeanmarc
Posts: 1822
Joined: Thu Aug 29, 2013 7:16 am

Re: 100% CPU after starting metern

Post by jeanmarc » Fri Mar 22, 2019 6:32 pm

ok i get it, i think. The issue is because /data/metern.err is too big to handle. Just remove it should do the trick.

mstuetz
Posts: 94
Joined: Sun Dec 29, 2013 1:20 pm

Re: 100% CPU after starting metern

Post by mstuetz » Fri Mar 22, 2019 6:35 pm

Damn. After adding a 3rd similar sensor, problem is back :(
Even when deleting metern.err cpu is @100% from php user www-data.

It's just 3 sensors with .sh scripts (main pooling only) containing one simple awk:

Code: Select all

awk '{OFS=""} {print "1(",$1,"*V)"}' /tmp/inv1/BATTV.txt
I cant believe that it has something to do as it is not even php.

What I get is:

Code: Select all

[Fri Mar 22 18:20:39.280371 2019] [:error] [pid 24669] [client 192.168.123.91:57918] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1372346064 bytes) in /var/www/html/metern/admin/help.php on line 134, referer: http://192.168.123.92/metern/admin/admin.php

mstuetz
Posts: 94
Joined: Sun Dec 29, 2013 1:20 pm

Re: 100% CPU after starting metern

Post by mstuetz » Fri Mar 22, 2019 6:40 pm

As soon as I enable debug, metern.err increases within secs. to infinitely.
What I could read out of it:
PHP Notice: Undefined variable: contalines in /var/www/html/metern/scripts/loadcfg.php on line 171
PHP Notice: Undefined variable: lines in /var/www/html/metern/scripts/loadcfg.php on line 166
PHP Notice: Undefined variable: contalines in /var/www/html/metern/scripts/loadcfg.php on line 171
PHP Notice: Undefined variable: lines in /var/www/html/metern/scripts/loadcfg.php on line 166
PHP Notice: Undefined variable: contalines in /var/www/html/metern/scripts/loadcfg.php on line 171
PHP Notice: Undefined variable: lines in /var/www/html/metern/scripts/loadcfg.php on line 166

jeanmarc
Posts: 1822
Joined: Thu Aug 29, 2013 7:16 am

Re: 100% CPU after starting metern

Post by jeanmarc » Fri Mar 22, 2019 7:44 pm

hmm, it's mN that don't like sensors.. :oops: remove again metern.err
Try this as scripts/loadcfg.php

Code: Select all

<?php
// Load configuration
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

if (get_current_user() == 'root') {
    //die('Abording. meterN cannot be as root');
}
// Few checks
$input = '{ "jsontest" : " <br>Json extension loaded" }';
$val   = json_decode($input, true);
if ($val["jsontest"] != "") {
} else {
    die("/!\ Json extension -NOT- loaded. Abording, please update php.ini.\n");
}

define('checkaccess', TRUE);
if (is_readable('../config/config_main.php')) {
    include('../config/config_main.php');
} else {
    die("Abording. Can't open config_main.php.\n");
}
if (is_readable('../config/config_indicator.php')) {
    include('../config/config_indicator.php');
} else {
    die("Abording. Can't open config_indicator.php.\n");
}
if (is_readable('../config/memory.php')) {
    include('../config/memory.php');
} else {
    die("Abording. Can't open memory.php.\n");
}
if (file_exists($MEMORY) && !is_writable($MEMORY)) {
    die("Abording. Can't write $MEMORY.php.\n");
}

if (version_compare(phpversion(), '7.1', '>=')) { // json_encode() uses EG(precision)
    ini_set('serialize_precision', -1);
}

if (file_exists($MEMORY)) {
    $data     = file_get_contents($MEMORY);
    $memarray = json_decode($data, true);
}

date_default_timezone_set($DTZ);
// Date check
$output = array();
$output = glob('../data/csv/*.csv');
sort($output);
$xdays = count($output);

$nowutc = strtotime(date('Ymd H:i:s'));
if ($xdays > 1) {
    $lastlog    = $output[$xdays - 1];
    $lines      = file($lastlog);
    $contalines = count($lines);
    $array      = preg_split('/,/', $lines[$contalines - 1]);
    $date1      = substr($output[$xdays - 1], -12);
    
    $year   = (int) substr($date1, 0, 4);
    $month  = (int) substr($date1, 4, 2);
    $day    = (int) substr($date1, 6, 2);
    $hour   = (int) substr($array[0], 0, 2);
    $minute = (int) substr($array[0], 3, 2);
    
    $epochdate = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
} else {
    $contalines = 0;
    $epochdate  = 1242975600;
}
$i = 1;
while ($nowutc < $epochdate) {
    $nowutc = strtotime(date('Ymd H:i:s'));
    echo "Computer time is not correct, trying again in $i sec\n";
    sleep("$i");
    $i++;
    if ($i > 1200) {
        die("Abording..\n");
    }
}

// Initialize variables
$minlist = array(
    '00',
    '05',
    '10',
    '15',
    '20',
    '25',
    '30',
    '35',
    '40',
    '45',
    '50',
    '55'
);

$DATADIR = dirname(dirname(__FILE__)) . '/data/';
$DELAY *= 1000;

function pushover($aid, $uid, $title, $msg) // Push-over
{
    curl_setopt_array($ch = curl_init(), array(
        CURLOPT_URL => 'https://api.pushover.net/1/messages.json',
        CURLOPT_TIMEOUT => 10,
        CURLOPT_POSTFIELDS => array(
            'token' => "$aid",
            'user' => "$uid",
            'message' => "$msg"
        )
    ));
    curl_exec($ch);
    curl_close($ch);
}

function rpinotify($token, $msg) // RpiNotify
{
    $post['text'] = $msg;
    $ch           = curl_init('https://api.rpinotify.it/message/' . $token . '/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $output = curl_exec($ch);
    curl_close($ch);
}

function logevents($stringData) // Log to events
{
    $dir = dirname(dirname(__FILE__)) . '/data/';
    $stringData .= file_get_contents($dir . 'events.txt');
    file_put_contents($dir . '/events.txt', $stringData);
}

function isvalid($id, $datareturn) //  IEC 62056 data set structure
{
    $regexp = "/^$id\(-?[A-z0-9\.]+\*[A-z0-9³²%°]+\)$/i"; //ID(VALUE*UNIT)
    if (preg_match($regexp, $datareturn)) {
        $datareturn = preg_replace("/^$id\(/i", '', $datareturn, 1); // VALUE*UNIT)
        $datareturn = preg_replace("/\*[A-z0-9³²%°]+\)$/i", '', $datareturn, 1); // VALUE
    } else {
        $datareturn = null;
    }
    return $datareturn;
}

// Meters config and memory variables
for ($i = 1; $i <= $NUMMETER; $i++) {
    if (is_readable("../config/config_met$i.php")) {
        include("../config/config_met$i.php");
    } else {
        die("Abording. Can't open config_met$i.php.\n");
    }
    if (!isset($memarray["msgflag$i"])) {
        $memarray["msgflag$i"] = 0;
    }
    $livememarray['UTC']               = strtotime(date('Ymd H:i:s'));
    $livememarray["${'METNAME'.$i}$i"] = 0;
    ${'comlost' . $i}                  = false;
    
    if (${'TYPE' . $i} != 'Sensor' && isset($output[0]) && (!isset($memarray["First$i"]) || !isset($memarray["Last$i"]))) {
        $val_last  = null;
        $val_first = null;
        $j         = 0;
        while (!isset($val_first)) {
            $j++;
            $array     = preg_split('/,/', $lines[$j]);
            $val_first = isset($array[$i]) ? trim($array[$i]) : '';
            if ($val_first == '') { // if skipped
                $val_first = null;
            }
            if ($j == $contalines - 1) {
                $val_first = 0; // didn't find any prev. first value
            }
        }
        $j = 0;
        while (!isset($val_last)) {
            $j++;
            $array    = preg_split('/,/', $lines[$contalines - $j]);
            $val_last = isset($array[$i]) ? trim($array[$i]) : '';
            if ($val_last == '') {
                $val_last = null;
            }
            if ($j == $contalines - 1) {
                $val_last = 0;
            }
        }
        $memarray["First$i"] = round($val_first, ${'PRECI' . $i});
        $memarray["Last$i"]  = round($val_last, ${'PRECI' . $i});
    }
}

if (!isset($memarray['5minflag'])) {
    $memarray['5minflag'] = false;
}

$myFile = dirname($DATADIR) . 'metern.err';
if (file_exists($myFile)) {
    $lines = file($myFile);
    $cnt   = count($lines);
    if ($cnt >= $AMOUNTLOG) {
        array_splice($lines, 0, $AMOUNTLOG);
        $file2 = fopen($myFile, 'w');
        fwrite($file2, implode('', $lines));
        fclose($file2);
    }
}

/////  Main memory
$data = json_encode($memarray);
file_put_contents($MEMORY, $data);
/////  Live memory
$data = json_encode($livememarray);
file_put_contents($LIVEMEMORY, $data);
?>

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests