0

我正在尝试提出一条 SQL 语句来打印 Puppet 数据库中所有重复的 [exported-resource] 定义。

mysql> SELECT id,restype,host_id,source_file_id FROM resources    
    -> WHERE title IN (SELECT title FROM resources WHERE exported=1 
    -> GROUP BY title HAVING count(title) > 1) ORDER BY title;
+------+------------------------+---------+----------------+
| id   | restype                | host_id | source_file_id |
+------+------------------------+---------+----------------+
|  305 | Nagios::Client::Export |       2 |             18 |
|  333 | Nagios_host            |       2 |             39 |
|  605 | Nagios_hostextinfo     |       6 |              2 |
|  443 | Nagios_hostextinfo     |       2 |             39 |
|  499 | Nagios_host            |       6 |              2 |
|  770 | Nagios::Client::Export |       6 |             18 |
......
......

哪个工作得很好,但是我如何从hosts表中检索/打印hosts.name而不是. 我只是无法通过重写上面的 SQL 语句来解决问题。主机表如下所示:host_id

mysql> SELECT id,name  FROM hosts;
+----+-----------------------------------------+
| id | name                                    |
+----+-----------------------------------------+
|  2 | controller-dns-01.sdas.cloud.com        |
|  6 | controller-monitoring-01.sdas.cloud.com |
|  1 | controller-puppet.sdas.cloud.com        |
| 13 | talend-admin-01.sdas.cloud.com          |
| 15 | talend-jobserver-01.sdas.cloud.com      |
| 14 | talend-jobserver-02.sdas.cloud.com      |
+----+-----------------------------------------+

另外,有没有办法只打印主机名的第一部分(即 only controller-dns-01)而不是完整的字符串?非常感谢任何人的任何建议。干杯!!


更新:
这是我的最终命令:以防万一其他人也在寻找一种方法来找出 Puppet Exported resources 重复定义

mysql> CREATE INDEX index_resources_on_restypetitle ON resources (restype(12),title(12));
mysql> SELECT r.id, r.restype, r.title, SUBSTRING_INDEX(h.name,'.',1) AS 'host_name',
    -> SUBSTRING_INDEX(s.filename,'puppet/',-1) AS 'file_name', r.line FROM resources r 
    -> LEFT JOIN hosts h ON r.host_id = h.id LEFT JOIN source_files s ON r.source_file_id = s.id 
    -> WHERE MD5(CONCAT(restype,title,host_id)) 
    -> IN (SELECT MD5(CONCAT(restype,title,host_id)) FROM resources 
    -> WHERE exported=1 GROUP BY MD5(CONCAT(restype,title,host_id)) 
    -> HAVING COUNT(MD5(CONCAT(restype,title,host_id))) > 1) ORDER BY title;

SUBSTRING_INDEX(s.filename....)位可能需要根据配置重新调整。非常感谢 thiella 帮助我。

4

1 回答 1

3

您需要将resources表与hosts表连接起来,使用 SUBSTRING_INDEX 来显示点左侧的字符串部分:

SELECT
  r.id, r.restype, r.host_id, r.source_file_id,
  SUBSTRING_INDEX(h.name, '.', 1)
FROM
  resources r LEFT JOIN hosts h
  ON r.host_id = h.id
WHERE
  r.title IN (SELECT title
            FROM resources
            WHERE export=1 
            GROUP BY title
            HAVING count(title) > 1)
ORDER BY
  r.title;
于 2013-04-06T10:09:15.710 回答