Uploaded image for project: 'ZABBIX BUGS AND ISSUES'
  1. ZABBIX BUGS AND ISSUES
  2. ZBX-17527

Dashbooard widget Problems by Severity to high CPU usage

XMLWordPrintable

    • Icon: Patch request Patch request
    • Resolution: Duplicate
    • Icon: Trivial Trivial
    • None
    • 4.4.7
    • Frontend (F)
    • None
    • debian linux/nginx/php-fpm, not really env dependant

      Steps to reproduce:

      1. Whenever there are quite a number of active events, widget Problems By Severity significantly degrades. Turning debugging in my case show that out of 180-200 seconds only 1.5 seconds is spent in SQL. System profiling shows that php-fpm utilizes the CPU a lot. Invistigation in the code showed that the most time is spent in blocks.inc.php
      2.  
        $actions = getEventsActionsIconsData($problems_data$data['triggers']);
         (blocks.inc.php line ~263)

      3. which in turn led to
        getEventsActionsin actions.inc.php, line ~1487

      pproblematic code is 
      foreach ($events as $event) {
      ....
      $event_actions = getSingleEventActions($event$r_events....
      ....

      at lines ~1530

      So, the problem: this code loops through all the events and calls getSingleEventActions function for each event.

      Inside getSingleEventActions there is a loop on all alerts.

      So in my case, there were about 1k of events and about 200k of alerts, which led to total loop count almost of 200M.

      So I suggest a simple fix by making alerts array indexed by eventd which speeds up the widget at least two-fold.

      In my case simple fix did this:
      $alerts_by_event=[];
          foreach ($alerts as $alert) {
              if (!in_array$alert['eventid'], $alerts_by_eventFALSE)) $alerts_by_event[$alert['eventid']]=[];
              array_push($alerts_by_event[$alert['eventid']],$alert);
          }
       

      And inside the loop call
      $event_actions = getSingleEventActions($event$r_events$alerts_by_event[$event['eventid']]);
       
      Result:
      100-200x performance increase, time spent in php drops from 180-200 seconds to 1-2 seconds

      Let me put fixed code altogether
      $alerts_by_event=[];
          foreach ($alerts as $alert) {
              if (!in_array$alert['eventid'], $alerts_by_eventFALSE)) $alerts_by_event[$alert['eventid']]=[];
              array_push($alerts_by_event[$alert['eventid']],$alert);
          }

          foreach ($events as $event) {
              //also adding empty alert arrays if there is no alerts for them
              if (!in_array$event['eventid'], $alerts_by_eventFALSE)) $alerts_by_event[$event['eventid']]=[];

              $event_actions = getSingleEventActions($event$r_events$alerts_by_event[$event['eventid']]);
              $actions[$event['eventid']] = [
                  'actions' => $event_actions['actions'],
                  'count' => $event_actions['count'],
                  'has_uncomplete_action' => $event_actions['has_uncomplete_action'],
                  'has_failed_action' => $event_actions['has_failed_action']
              ];
              $mediatypeids += $event_actions['mediatypeids'];
              $userids += $event_actions['userids'];
          }

            Unassigned Unassigned
            makurov Mikhail Makurov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: