Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql malformed where clause #37

Closed
joernott opened this issue Aug 23, 2019 · 6 comments
Closed

sql malformed where clause #37

joernott opened this issue Aug 23, 2019 · 6 comments
Milestone

Comments

@joernott
Copy link

I have defined the following yaml file:

name: Spirit
children:
- name: dev-sys0
  children:
  - name: db2
    children:
    - host:    'dev-sys0-db2-v01.sf-rz.de'
      type:    host
    - service: 'db2_health_tablespace-usage'
      host:    'dev-sys0-db2-v01.sf-rz.de'
      type:    service

When trying to view this, I am getting this error message;

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "("
LINE 8: AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1
^, query was: SELECT so.name1 AS host_name, so.name2 AS service_description, CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE CASE WHEN ss.state_type = 1 THEN ss.current_state ELSE ss.last_hard_state END END AS service_hard_state, CASE WHEN (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END AS service_handled, CASE WHEN ss.problem_has_been_acknowledged > 0 THEN 1 ELSE 0 END AS service_handled_wo_host, ss.notifications_enabled AS service_notifications_enabled, ntpo.name1 AS service_notification_period, ss.is_flapping AS service_is_flapping, CASE WHEN (ss.scheduled_downtime_depth = 0 OR ss.scheduled_downtime_depth IS NULL) THEN 0 ELSE 1 END AS service_in_downtime FROM icinga_objects AS so
INNER JOIN icinga_services AS s ON s.service_object_id = so.object_id AND so.is_active = 1 AND so.objecttype_id = 2
INNER JOIN icinga_hoststatus AS hs ON hs.host_object_id = s.host_object_id
INNER JOIN icinga_servicestatus AS ss ON ss.service_object_id = so.object_id
LEFT JOIN icinga_timeperiods AS ntp ON ntp.timeperiod_object_id = s.notification_timeperiod_object_id AND ntp.config_type = 1 AND ntp.instance_id = s.instance_id
LEFT JOIN icinga_objects AS ntpo ON ntpo.object_id = s.notification_timeperiod_object_id
LEFT JOIN icinga_timeperiod_timeranges AS ntpr ON ntpr.timeperiod_id = ntp.timeperiod_id
AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1
AND ntpr.start_sec < UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
AND ntpr.end_sec > UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
WHERE (so.name1 IN ('dev-sys0-db2-v01.sf-rz.de')) ORDER BY LOWER(s.display_name) ASC

It look slike the WHERE clause is generated in a wrong order, the lines starting with "AND" are listed before the line starting with "WHERE".

This happens when I define a service. When I remove the service object from the yaml file, the tile is displayed, but it is in light purple (despite the hostalive for that host being green), but that is another issue.

@ulyszk
Copy link

ulyszk commented Jan 8, 2020

It seems I have same issue. Could we somehow fix it?

@KAMI911
Copy link

KAMI911 commented Mar 17, 2020

I have exactly same issue.

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "(" LINE 8: AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1 ^, query was: SELECT so.name1 AS host_name, so.name2 AS service_description, CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE CASE WHEN ss.state_type = 1 THEN ss.current_state ELSE ss.last_hard_state END END AS service_hard_state, CASE WHEN (ss.problem_has_been_acknowledged + ss.scheduled_downtime_depth + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END AS service_handled, CASE WHEN ss.problem_has_been_acknowledged > 0 THEN 1 ELSE 0 END AS service_handled_wo_host, ss.notifications_enabled AS service_notifications_enabled, ntpo.name1 AS service_notification_period, ss.is_flapping AS service_is_flapping, CASE WHEN (ss.scheduled_downtime_depth = 0 OR ss.scheduled_downtime_depth IS NULL) THEN 0 ELSE 1 END AS service_in_downtime FROM icinga_objects AS so INNER JOIN icinga_services AS s ON s.service_object_id = so.object_id AND so.is_active = 1 AND so.objecttype_id = 2 INNER JOIN icinga_hoststatus AS hs ON hs.host_object_id = s.host_object_id INNER JOIN icinga_servicestatus AS ss ON ss.service_object_id = so.object_id LEFT JOIN icinga_timeperiods AS ntp ON ntp.timeperiod_object_id = s.notification_timeperiod_object_id AND ntp.config_type = 1 AND ntp.instance_id = s.instance_id LEFT JOIN icinga_objects AS ntpo ON ntpo.object_id = s.notification_timeperiod_object_id LEFT JOIN icinga_timeperiod_timeranges AS ntpr ON ntpr.timeperiod_id = ntp.timeperiod_id AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1 AND ntpr.start_sec < UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE()) AND ntpr.end_sec > UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE()) WHERE ((so.name1 IN ('her-new.example.com', 'sys-mysql.example.com', 'haproxy01.example.com', 'haproxy02.example.com', 'her-test.si.net'))) ORDER BY LOWER(s.display_name) ASC

@ulyszk
Copy link

ulyszk commented Mar 17, 2020

It seems the problem is caused by the static parts of SQL queries, which is matches with MySQL but incompatible with PostgreSQL. @joernott are you using PostgreSQL with Icinga2?

@KAMI911
Copy link

KAMI911 commented Mar 17, 2020

I tried to patch it to work with PostgreSQL:

diff --git a/library/Toplevelview/Monitoring/HostgroupQuery.php b/library/Toplevelview/Monitoring/HostgroupQuery.php
index 62cc014..b363b42 100644
--- a/library/Toplevelview/Monitoring/HostgroupQuery.php
+++ b/library/Toplevelview/Monitoring/HostgroupQuery.php
@@ -122,9 +122,9 @@ class HostgroupQuery extends IcingaHostgroupQuery
         $this->select->joinLeft(
             ['ntpr' => $this->prefix . 'timeperiod_timeranges'],
             "ntpr.timeperiod_id = ntp.timeperiod_id
-                AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1
-                AND ntpr.start_sec <= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
-                AND ntpr.end_sec >= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
+                AND ntpr.day = EXTRACT(DOW FROM CURRENT_DATE - 1)
+                -- AND ntpr.start_sec < EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM CURRENT_DATE)
+                -- AND ntpr.end_sec > EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM CURRENT_DATE)
             ",
             []
         );
diff --git a/library/Toplevelview/Monitoring/ServicestatusQuery.php b/library/Toplevelview/Monitoring/ServicestatusQuery.php
index addee2f..1725a1e 100644
--- a/library/Toplevelview/Monitoring/ServicestatusQuery.php
+++ b/library/Toplevelview/Monitoring/ServicestatusQuery.php
@@ -77,9 +77,9 @@ class ServicestatusQuery extends IcingaServicestatusQuery
         $this->select->joinLeft(
             ['ntpr' => $this->prefix . 'timeperiod_timeranges'],
             'ntpr.timeperiod_id = ntp.timeperiod_id
-                AND ntpr.day = DAYOFWEEK(CURRENT_DATE()) - 1
-                AND ntpr.start_sec < UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
-                AND ntpr.end_sec > UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CURRENT_DATE())
+                AND ntpr.day = EXTRACT(DOW FROM CURRENT_DATE - 1)
+                -- AND ntpr.start_sec < EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM CURRENT_DATE)
+                -- AND ntpr.end_sec > EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM CURRENT_DATE)
             ',
             []
         );

Now it is not break on PostgreSQL, might not work on MySQL/MariaDB. or not work at all. Try to figure out how can be DB independent.

@joernott
Copy link
Author

Yes, we are working with Postgresql

@martialblog martialblog added this to the 0.4.0 milestone May 13, 2024
@martialblog
Copy link
Member

Should be fixed in #54 which now uses Icingadb\Common\Database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants