php exec() return wrong data - php

When I execute php script with exec('vnstat -tr', $results);
Output:
Array ( [0] => Sampling eth0 (5 seconds average)... 0 packets sampled in 5 seconds
[1] => Traffic average for eth0
[2] =>
[3] => rx 0.00 kbit/s 0 packets/s
[4] => tx
0.00 kbit/s 0 packets/s
[5] => )
When I execute vnstat -tr in console.
Output look like this:
root#s2:~# vnstat -tr 313727 packets sampled in 5 seconds Traffic average for eth0
rx 10,42 Mbit/s 20092 packets/s
tx 488,22 Mbit/s 42653 packets/s
Someone Could tell my why PHP returned wrong data?

Related

Exporting a large CSV via PHP results in timeout/memory allocate error

I have a tool that allows data to be exported based on different time ranges. The problem I run into is that the longer the time range, the more data, and eventually the data set is too large -- resulting in either a timeout error or a memory allocation error.
Short of changing php.ini to have a larger max_execution_time, etc, is there a better way of doing this?
Here's my script to build the CSV (the user selects export, then this page is loaded):
$fileName = "export-".date("YmdHis");
$exportSignature = array(" ","Export","Generated by: ".$_SESSION['name'],date("Y-m-d H:i:s"));
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$fileName.'.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// run all the queries for the data
if($exportType == "list") {
include('include/query-compare.php');
} elseif($exportType == "analyze") {
include('include/query-analyze.php');
} elseif($exportType == "funnel") {
include('include/query-funnel.php');
} elseif($exportType == "funnelTrend") {
include('include/query-funnel-trends.php');
}
// add column headers
fputcsv($output,$columnHeaders);
// add row data
if($exportType == "list") {
foreach($comparison as $account) {
fputcsv($output,$account);
}
} elseif($exportType == "analyze") {
foreach($analyze as $account) {
fputcsv($output,$account);
}
} elseif($exportType == "funnel" || $exportType == "funnelTrend") {
foreach($funnel as $line) {
fputcsv($output,$line);
}
}
// add export signature
foreach($exportSignature as $line) {
fputcsv($output,array($line));
}
Here's a sample of the array that's used to create the rows. This is the variable -- if the time range is small there might be a few hundred entries in the array, if it's large, there's tens of thousands.
Array
(
[0] => Array
(
[0] => 1
[firstName] => Marco
[lastName] => R.
[title] => D
[email] => test#test.com
[company] => xx
[lSource] => xx
[lDetail] => xx
[lExact] => xx
[createdAt] => 2017-06-26 00:00:00
[nDate] => 2017-08-15
[cDate] => 2017-08-15
[cMoment] => xx
[mDate] => 2017-08-15
[mMoment] => xx
[Id] => 003Axx
[Type] => Contact
[accountId] => 001A0xx
[parentAccountId] =>
[accountOwner] => Kevin S.
[xx] => Nicholas W.
[accountType] => Prospect
[totalARR] => 0.00
[accountTier] =>
[ty] => XX
[industry] => Corporate Services
[secondaryIndustry] => IT Services and Consulting
[billingCountry] => Germany
[1] => 006Axx
[2] => PS (New Business)
[3] => Kevin S.
[4] => Nicholas W.
[5] => cc
[6] => New Business
[7] => Identify
[8] => 40000.00
[9] => 2017-08-16
[10] => 2018-07-27
[11] => 2017-08-16
[12] => 2017-08-21
)
)
I'd be fine with scheduling the export and emailing it to the user as well, I'm just not familiar with how I can have this large CSV constructed in the background without timing out.
EDIT: would it be a better option to queue the export in a database and have a cron job that looks for the exports, then runs the export in the background until complete? Since it's running via the cron it shouldn't have a timeout, right?
Use set_time_limit ( 0 ); at the beginning of your file. For example:
set_time_limit ( 0 );
$fileName = "export-".date("YmdHis");
Using set_time_limit ( 0 ); didn't help since I'm running on nginx and would run into gateway errors. Instead, I wound up queuing the downloads into a database table and executing the actual queries via a cron job, as those aren't subject to the same timeout limitations.

How to integrate PHP and R on Windows?

Having some issues integrating PHP and R. I am working from this article:
http://www.r-bloggers.com/integrating-php-and-r/
R is installed and is verified working with our R script:
Rscript C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2
Printing a single value that is a result of its calculations:
[1] "0"
(The path to Rscript.exe is set in the Windows environmental variables)
I have a PHP script in place that is using exec() which tests successfully with commands such as:
$result = exec('dir',$output,$returnVar);
echo "<br>result ". print_r($result,true);
echo "<br>output <pre>". print_r($output,true) , "</pre>";
echo "<br>return ". print_r($returnVar,true);
returning:
result 2 Dir(s) 117,749,354,496 bytes free
output
Array
(
[0] => Volume in drive C is C_DRIVE
[1] => Volume Serial Number is 7EB2-A074
[2] =>
[3] => Directory of C:\inetpub\wwwroot\client\temp
[4] =>
[5] => 05/17/2014 10:29 PM .
[6] => 05/17/2014 10:29 PM ..
[7] => 05/16/2014 09:24 AM 5,181 dbimporttest.php
[8] => 05/17/2014 10:29 PM 0 routput.txt
[9] => 05/17/2014 11:42 PM 701 rscripttest.php
[10] => 05/16/2014 04:59 PM 425 whoami.php
[11] => 4 File(s) 6,307 bytes
[12] => 2 Dir(s) 117,749,354,496 bytes free
)
return 0
When I try to run the R script within the exec command, it fails:
$result = exec('Rscript.exe C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2',$output,$returnVar);
echo "<br>result ". print_r($result,true);
echo "<br>output <pre>". print_r($output,true) , "</pre>";
echo "<br>return ". print_r($returnVar,true);
Returning:
result
output
Array
(
)
return 1
I am running:
Windows Server 8 R2
IIS 8
PHP 5.5
R 3.1
Unable to get exec() to work or output errors that are usable, I decided to seek an alternative route. Using the COM class seems to have given me what I was looking for.
Here is the final, operational code:
$command = 'C:\Program Files\R\R-3.1.0\bin\Rscript.exe C:\inetpub\wwwroot\client\includes\decisionTreePredictor.R 20 10 O 1000 10000 5000 0.2 10.2';
$pCom = new COM("WScript.Shell");
$pShell = $pCom->Exec($command);
$sStdOut = $pShell->StdOut->ReadAll; # Standard output
$sStdErr = $pShell->StdErr->ReadAll; # Error
echo "<pre>$sStdOut</pre>";
Odd that I couldn't get exec() to do the job as that seems be the solution preferred by most bloggers discussing R/PHP integration.
Anyway, I hope this solution helps anyone else that finds themselves in my situation!
P.S. You will want to make sure the extension is on in php.ini (it is off by default on install): extension=php_com_dotnet.dll

Insert into array by spaces

I have this TXT file, which each line I would to insert into array, like this:
Array ( [0] => [1] => 8100 [2] => 623 [3] => 09:04 [4] => AM [5] => 00:26 [6] => L [7] => S-ED [8] => 768 [9] => #4506856439 [10] => 00:01 [11] => )
CO USER TIME DURATION TYPE ACCOUNT_CODE CALLED_NUM RING_TIME
8100 623 09:04 AM 00:26 L S-E 250821613987
8021 8816 09:06 AM 00:20 I S-E D 768 #4506856439 00:01
8020 09:06 AM I N D 603 #45424044499 00:30
8011 09:07 AM 00:11 I S-E D 727 #7546355292 00:02
" 8817 00:11
" 00:02 H
8100 623 09:07 AM 00:01 L S-E 5542204034
8007 8818 09:08 AM 00:13 I S-E D 618 #45269021726 00:01
8013 8811 09:09 AM 00:01 I S-E D 770 #436217227 00:01
8014 09:10 AM 00:16 I S-E D 652 #4523859922 00:01
I'd like to insert it into an array with all parameters, even if empty,
but I can't get it work.
I tried this:
$fh = fopen('captures/131210.txt','r');
while ($line = fgets($fh)) {
$tempexp = preg_split('/\s+/', $line);
print_r($tempexp);
echo "<br />";
}
fclose($fh)
But it gives out weird outputs for some values, like this:
Array ( [0] => [1] => " [2] => 8812 [3] => 00:16 [4] => )
Help would be much appreciated!
If the layout of your text file is purely based on spaces you can used the exact columns to extract the data. Since the columns are not of equal width you cannot simply write it in one loop.
Use the substr function to get part of the line (first number is index, second number is length to read)
$fh = fopen('captures/131210.txt','r');
while ($line = fgets($fh)) {
$co = substr($line, 0, 7);
$user = substr($line, 7, 6);
... etc ...
echo "<br />";
}
fclose($fh)
You might need to parse the separate parts further down dependent on the content and what you want to do with it - parse as integer, date, time etc.

Can't read csv(Tab delimited) properly

I have simple csv file which is tab delimited which i have to use as it is because it is coming from somewhere and i hvae to read it and insert it into my db i have used a simple php code to read it
if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){
/*Skip the first row*/
fgetcsv($handle, 1000,chr(9));
while(($data = fgetcsv($handle,1000,chr(9)))!==FALSE){
print_r($data[0]);
}
}
When print_r the data it shows like
Array ( [0] => 01SATAPC [1] => 40ATAPC [2] => [3] => 21P [4] => SERIAL ATA POWER CABLE [5] => 0.00 [6] => 2.00 [7] => 0 [8] => Power Supplies [9] => SERIAL ATA POWER CABLE [10] =>
4 TO 15 PIN 160MM
[11] => [12] => [13] => [14] => MELBHO [15] => 0.000 [16] => [17] => Order to Order [18] => 4 [19] => 2013-01-18 )
Which is the desired result but when i go to access the particular column value using the $data['index'] e.g. $data[8] or $data[1] it weirdly giving me garbage values says for some iterations it give me right values but after 10-15 rows its starting giving me the some numbers and other column values..... i don't know whats is going on with this as far as i know it should be formatting issue i have tried open the file in excel and its coming fine....
#ravisoni are you sure that the second parameter to fgetcsv of 1000 is longer than the longest line in your file? Try setting it to 0 as the docs say [php.net/fgetcsv] and see if that makes a difference.
if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){
/*Skip the first row*/
fgetcsv($handle, 0,chr(9));
while(($data = fgetcsv($handle,0,chr(9)))!==FALSE){
print_r($data[0]);
}
}

How to extract data from df -h in php?

I need to build a script that will query a remote storage server with multiple drives for their free space.
I simply login with SSH, run df and throw the output into a variable. Which looks like this:
Array
(
[0] => Filesystem 1K-blocks Used Available Use% Mounted on
[1] => /dev/sda1 59392320 14949240 41426064 27% /
[2] => /dev/sdd1 140592240 114503840 18946708 86% /home/overflow
[3] => /dev/sdf1 287826944 273844324 0 100% /home/node2
[4] => /dev/sde1 287826944 253278964 19927228 93% /home/node3
[5] => /dev/sdg1 287826944 4771768 268434424 2% /home/node4
[6] => /dev/sdh1 287826944 4329780 268876412 2% /home/node5
[7] => /dev/sdi1 488302976 439077756 24420864 95% /home/node1
[8] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode6
[9] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode7
[10] => tmpfs 3145728 0 3145728 0% /tmp
)
I need to extract how much free space each "node" or "mnode" has. So perhaps a nice array that has pairs such as this:
mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000
Use explode(' ', preg_replace('/ +/', ' ', $line)) to get an array with all values split up.

Categories