使用 PHP 解析 BackupPC 的输出

发布于 2025-01-10 13:37:46 字数 798 浏览 0 评论 0原文

我尝试解析“BackupPC_serverMesg status Hosts”的输出。

我得到的输出:

Got reply: %Status = (" admin " => {"endTime" => "1645958205","activeJob" => 0,"state" => "Status_idle"},"192.168.1.50" => {"lastGoodBackupTime" => "1645944158","deadCnt" => 0,"reason" => "Reason_nothing_to_do","activeJob" => 0,"state" => "Status_idle","aliveCnt" => 1560,"endTime" => "","needLink" => 0,"startTime" => "1645970401","type" => "incr","userReq" => undef}," trashClean " => {"endTime" => "1642317435","activeJob" => 1,"state" => "Status_link_running"}," admin1 " => {"endTime" => "1645956676","activeJob" => 0,"state" => "Status_idle"});

我不知道如何解析它。 我需要来自“192.168.1.50”的所有内容。

你知道如何解决吗?

谢谢你的帮助

马茨

i try to parse the output from "BackupPC_serverMesg status hosts".

The output i get:

Got reply: %Status = (" admin " => {"endTime" => "1645958205","activeJob" => 0,"state" => "Status_idle"},"192.168.1.50" => {"lastGoodBackupTime" => "1645944158","deadCnt" => 0,"reason" => "Reason_nothing_to_do","activeJob" => 0,"state" => "Status_idle","aliveCnt" => 1560,"endTime" => "","needLink" => 0,"startTime" => "1645970401","type" => "incr","userReq" => undef}," trashClean " => {"endTime" => "1642317435","activeJob" => 1,"state" => "Status_link_running"}," admin1 " => {"endTime" => "1645956676","activeJob" => 0,"state" => "Status_idle"});

I don´t have a Idea how i can parse it.
I need all from "192.168.1.50".

Do you have i idea how to solve it?

thank you for help

Matze

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2025-01-17 13:37:46

根据文档 https://backuppc.github.io/backuppc/ BackupPC_serverMesg 命令您始终会获得输出在 Perl hash 的文本版本中,您需要在 Perl 中进行 eval() ,然后只有您可以在 PHP 中获得 JSON 格式。

以下是主机的 cmd 输出之一:-

bin/BackupPC_serverMesg status jobs
bin/BackupPC_serverMesg status hosts
bin/BackupPC_serverMesg status info
bin/BackupPC_serverMesg status queues

我编写了一些代码来解析 PHP

<?php //php 7.2.24

$code = 'Got reply: %Status = (" admin " => {"endTime" => "1645958205","activeJob" => 0,"state" => "Status_idle"},"192.168.1.50" => {"lastGoodBackupTime" => "1645944158","deadCnt" => 0,"reason" => "Reason_nothing_to_do","activeJob" => 0,"state" => "Status_idle","aliveCnt" => 1560,"endTime" => "","needLink" => 0,"startTime" => "1645970401","type" => "incr","userReq" => undef}," trashClean " => {"endTime" => "1642317435","activeJob" => 1,"state" => "Status_link_running"}," admin1 " => {"endTime" => "1645956676","activeJob" => 0,"state" => "Status_idle"});';

// Let's fix this to JSON String 

$code = str_replace('Got reply: %Status = ','',$code);
$code = str_replace('(','{',$code);
$code = str_replace(')','}',$code);
$code = str_replace('=>',':',$code);
$code = str_replace(';','',$code);
$code = stripslashes($code);
$code = str_replace('" ', '"',$code);
$code = str_replace(' "', '"',$code);
$code = str_replace('undef', '"undef"',$code);
$code = trim($code);

$data = json_decode($code, true);

print_r($data["192.168.1.50"]);

?>

输出中的此输出字符串:

Array
(
    [lastGoodBackupTime] => 1645944158
    [deadCnt] => 0
    [reason] => Reason_nothing_to_do
    [activeJob] => 0
    [state] => Status_idle
    [aliveCnt] => 1560
    [endTime] => 
    [needLink] => 0
    [startTime] => 1645970401
    [type] => incr
    [userReq] => undef
)

此外,要在 Perl 中对日志的所有输出进行此解析,请检查此存储库代码 https://github.com/plepe/backuppc-print-status/blob/master/backuppc-status-json

As per docs https://backuppc.github.io/backuppc/ BackupPC_serverMesg Commands you always get output in text version of perl hash that you need to be eval() inside Perl, then only you can get JSON format in PHP.

Following is one of the cmds output from hosts:-

bin/BackupPC_serverMesg status jobs
bin/BackupPC_serverMesg status hosts
bin/BackupPC_serverMesg status info
bin/BackupPC_serverMesg status queues

I wrote some code to parse this output string in PHP

<?php //php 7.2.24

$code = 'Got reply: %Status = (" admin " => {"endTime" => "1645958205","activeJob" => 0,"state" => "Status_idle"},"192.168.1.50" => {"lastGoodBackupTime" => "1645944158","deadCnt" => 0,"reason" => "Reason_nothing_to_do","activeJob" => 0,"state" => "Status_idle","aliveCnt" => 1560,"endTime" => "","needLink" => 0,"startTime" => "1645970401","type" => "incr","userReq" => undef}," trashClean " => {"endTime" => "1642317435","activeJob" => 1,"state" => "Status_link_running"}," admin1 " => {"endTime" => "1645956676","activeJob" => 0,"state" => "Status_idle"});';

// Let's fix this to JSON String 

$code = str_replace('Got reply: %Status = ','',$code);
$code = str_replace('(','{',$code);
$code = str_replace(')','}',$code);
$code = str_replace('=>',':',$code);
$code = str_replace(';','',$code);
$code = stripslashes($code);
$code = str_replace('" ', '"',$code);
$code = str_replace(' "', '"',$code);
$code = str_replace('undef', '"undef"',$code);
$code = trim($code);

$data = json_decode($code, true);

print_r($data["192.168.1.50"]);

?>

Output:

Array
(
    [lastGoodBackupTime] => 1645944158
    [deadCnt] => 0
    [reason] => Reason_nothing_to_do
    [activeJob] => 0
    [state] => Status_idle
    [aliveCnt] => 1560
    [endTime] => 
    [needLink] => 0
    [startTime] => 1645970401
    [type] => incr
    [userReq] => undef
)

Moreover to do this parse in Perl for all output from logs, check this repo code https://github.com/plepe/backuppc-print-status/blob/master/backuppc-status-json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文