0

我是 incinga2 框架的新手,如何在 incinga2 中运行 sql raw 或自定义查询,如下所示

SELECT 
   `user`,
   COUNT(0) as Total_Count,
   MAX(range_count) Max_Range_Count
FROM (
   SELECT
       a.`user`, 
       a.change_time, 
       COUNT(0) range_count
   FROM audit_log a
   INNER JOIN audit_log b ON a.`user` = b.`user` 
   WHERE b.change_time BETWEEN a.change_time AND a.change_time + INTERVAL 30 MINUTE
   GROUP BY a.`user`, a.change_time
) AS user_range_count
GROUP BY `user`

icinga2 中的这个数据库对象,用于连接数据库和获取数据。他们使用适配器,但 pdo 是空的。A 可以在这里添加我的 pdo 吗?icniga2框架对吗?

ipl\Sql\Connection Object
(
    [config:protected] => ipl\Sql\Config Object
        (
            [db] => mysql
            [host] => localhost
            [port] => 3306
            [dbname] => auditlog
            [username] => root
            [password] => 
            [charset] => utf8
            [options] => 
            [type] => db
            [persistent] => 0
        )

    [pdo:protected] => 
    [queryBuilder:protected] => 
    [adapter:protected] => ipl\Sql\Adapter\Mysql Object
        (
            [quoteCharacter:protected] => Array
                (
                    [0] => `
                    [1] => `
                )

            [escapeCharatcer:protected] => ``
            [escapeCharacter:protected] => \"
            [options:protected] => Array
                (
                    [8] => 0
                    [20] => 
                    [3] => 2
                    [11] => 0
                    [17] => 
                )

        )

    [pluginLoaders:protected] => Array
        (
            [adapter] => Array
                (
                    [0] => ipl\Stdlib\Loader\AutoLoadingPluginLoader Object
                        (
                            [namespace:protected] => ipl\Sql\Adapter
                            [postfix:protected] => 
                            [uppercaseFirst:protected] => 1
                        )

                )

        )

)
4

2 回答 2

0

您可以使用PDO

非常好的文章开始使用 PDO。

  1. 如何使用 PDO 连接到 MySQL
  2. 使用 PDO 选择查询
$strQuery = "SELECT 
   `user`,
   COUNT(0) as Total_Count,
   MAX(range_count) Max_Range_Count
FROM (
   SELECT
       a.`user`, 
       a.change_time, 
       COUNT(0) range_count
   FROM audit_log a
   INNER JOIN audit_log b ON a.`user` = b.`user` 
   WHERE b.change_time BETWEEN a.change_time AND a.change_time + INTERVAL 30 MINUTE
   GROUP BY a.`user`, a.change_time
) AS user_range_count
GROUP BY `user`";

foreach ($conn->query($strQuery) as $row) {
    print_r($row);
}
于 2019-09-12T05:36:55.943 回答
0

首先在 incinga 中安装任何数据库资源模块,例如Icinga Web 2 - IPL ,它将帮助我们在 incinga 中调用 pdo 查询,然后您可以查看其文档如何运行任何带有绑定参数的语句。例子:

use ipl\Sql\Select;
use PDO;
$stmt = 'Select * from table where id=?';
$id=1;
$execute=$this->dbConnection->fetchAll($stmt,[$id]);
于 2019-09-13T06:22:37.000 回答