查询otrs数据库

时间:2012-11-05 17:52:23

标签: sql postgresql otrs

我在从OTRS在postgresql数据库上创建查询时遇到问题。我需要获取一个资产列表,其中包含存储在CMDB中的一些数据,但我在存储数据方面遇到了麻烦。

这个查询可以正常工作,但只要我添加另一个左连接来获取更多数据,它就会一直运行而不会返回任何结果。

据我所知,问题是所有信息都包含在xml_storage表中而没有任何关系,因此我需要反复查询该表以获取数据。还有一种混合的数据类型,在某些情况下,整数存储为可变字符,这使我无法使用该值从常规目录中获取其他信息。

我这几天一直在用砖头撞墙,所以我真的很感激任何帮助

SELECT 
      asset_value,
      manufacturer_value,
      model_value,
      serial_value,
      fqdn_value,
      array_agg(service),
      purchasedate_value,
      status_value,
      configitem.configitem_number,
      configitem_version.name,
      general_catalog.name

    FROM 
      public.configitem_version,
      public.general_catalog,
      public.configitem 

    left join
      (SELECT
      xml_storage.xml_content_value AS asset_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''Asset Number''}[1]{''Content''}')   as select_asset on last_version_id::int = select_asset.xml_key::int

    left join
      (SELECT
      xml_storage.xml_content_value AS manufacturer_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''Manufacturer''}[1]{''Content''}' ) as select_manufacturer on last_version_id::int = select_manufacturer.xml_key::int

    left join
      (SELECT
      xml_storage.xml_content_value AS model_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''Model''}[1]{''Content''}' ) as select_model on last_version_id::int = select_model.xml_key::int

    left join
      (SELECT
      xml_storage.xml_content_value AS serial_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''SerialNumber''}[1]{''Content''}' ) as select_serial on last_version_id::int = select_serial.xml_key::int

    left join
      (SELECT
      xml_storage.xml_content_value AS fqdn_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''FQDN''}[1]{''Content''}' ) as select_fqdn on last_version_id::int = select_fqdn.xml_key::int

    left join
      (SELECT 
      link_relation.source_key AS "configitem_id", 
      service.name AS "service"
      FROM 
      public.link_relation, 
      public.service
      WHERE 
      link_relation.target_key::int = service.id AND
      link_relation.target_object_id::int = 2 AND link_relation.source_object_id::int = 3
      UNION
      SELECT 
      link_relation.target_key AS "configitem_id", 
      service.name AS "service"
      FROM 
      public.link_relation, 
      public.service
      WHERE 
      link_relation.source_key::int = service.id AND
      link_relation.target_object_id::int = 3 AND link_relation.source_object_id::int = 2) as select_service on configitem.id = configitem_id::int

    left join
      (SELECT
      xml_storage.xml_content_value AS purchasedate_value,
      xml_storage.xml_key
      FROM
      public.xml_storage 
      WHERE
      xml_content_key = '[1]{''Version''}[1]{''PurchaseDate''}[1]{''Content''}' ) as select_purchasedate on last_version_id::int = select_purchasedate.xml_key::int

    left join
      (SELECT
      general_catalog.name AS status_value,
      general_catalog.id
      FROM
      public.general_catalog) as select_status on configitem.cur_depl_state_id::int = select_status.id::int

    WHERE
      configitem.last_version_id = configitem_version.id AND
      configitem.class_id = general_catalog.id AND
      (configitem.class_id = 32 OR configitem.class_id = 33)


    GROUP BY
      asset_value,
      manufacturer_value,
      model_value,
      serial_value,
      fqdn_value,
      purchasedate_value,
      status_value,
      configitem.configitem_number,
      configitem_version.name,
      general_catalog.name

    ;

2 个答案:

答案 0 :(得分:1)

您是否可以使用OTRS API而不是查询?以这种方式获取数据会更容易一些。 此外,ImportExport模块允许将数据导出到.csv文件;根据您的需要,这可能是一种选择。

答案 1 :(得分:0)

我最终创建了一个快速且肮脏的Web抓取脚本,以获取所需的数据。如前所述,一个更好的方法是安装导入/导出库功能,但是具有添加扩展名所需技能和权限的人员被捆绑了。 https://github.com/OTRS/ImportExport

https://github.com/colif/Python_Utils/blob/master/OTRS-Web-Scraper-Clean.py