diff options
-rw-r--r-- | create/schema/schema.sql | 124 | ||||
-rw-r--r-- | include/common.h | 3 | ||||
-rw-r--r-- | src/libs/zbxcommon/str.c | 9 | ||||
-rw-r--r-- | src/zabbix_agent/logfiles.c | 4 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/history.c | 202 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/history.h | 2 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/nodecomms.h | 3 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/nodesender.c | 637 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/nodesender.h | 14 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/nodewatcher.c | 642 | ||||
-rw-r--r-- | src/zabbix_server/nodewatcher/nodewatcher.h | 13 | ||||
-rw-r--r-- | src/zabbix_server/trapper/nodehistory.c | 181 | ||||
-rw-r--r-- | src/zabbix_server/trapper/nodesync.c | 4 | ||||
-rw-r--r-- | src/zabbix_server/trapper/trapper.c | 9 |
14 files changed, 889 insertions, 958 deletions
diff --git a/create/schema/schema.sql b/create/schema/schema.sql index fc7bfae6..82fb2d44 100644 --- a/create/schema/schema.sql +++ b/create/schema/schema.sql @@ -22,12 +22,12 @@ -- TABLE|slideshows|slideshowid|ZBX_SYNC -FIELD |slideshowid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |slideshowid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |delay |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|slides|slideid|ZBX_SYNC -FIELD |slideid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |slideid |t_id |'0' |NOT NULL |0 FIELD |slideshowid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |screenid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |step |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -35,7 +35,7 @@ FIELD |delay |t_integer |'0' |NOT NULL |ZBX_SYNC INDEX |slides_1 |slideshowid TABLE|drules|druleid|ZBX_SYNC -FIELD |druleid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |druleid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |iprange |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |delay |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -43,7 +43,7 @@ FIELD |nextcheck |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |status |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|dchecks|dcheckid|ZBX_SYNC -FIELD |dcheckid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |dcheckid |t_id |'0' |NOT NULL |0 FIELD |druleid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |key_ |t_varchar(255) |'0' |NOT NULL |ZBX_SYNC @@ -51,7 +51,7 @@ FIELD |snmp_community |t_varchar(255) |'0' |NOT NULL |ZBX_SYNC FIELD |ports |t_varchar(255) |'0' |NOT NULL |ZBX_SYNC TABLE|dhosts|dhostid|ZBX_SYNC -FIELD |dhostid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |dhostid |t_id |'0' |NOT NULL |0 FIELD |druleid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |ip |t_varchar(39) |'' |NOT NULL |ZBX_SYNC FIELD |status |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -59,7 +59,7 @@ FIELD |lastup |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |lastdown |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|dservices|dserviceid|ZBX_SYNC -FIELD |dserviceid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |dserviceid |t_id |'0' |NOT NULL |0 FIELD |dhostid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |key_ |t_varchar(255) |'0' |NOT NULL |ZBX_SYNC @@ -76,7 +76,7 @@ FIELD |field_name |t_varchar(64) |'' |NOT NULL |0 FIELD |nextid |t_id |'0' |NOT NULL |0 TABLE|httptest|httptestid|ZBX_SYNC -FIELD |httptestid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |httptestid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |applicationid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |lastcheck |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -92,7 +92,7 @@ FIELD |time |t_double |'0' |NOT NULL |ZBX_SYNC FIELD |error |t_varchar(255) |'' |NOT NULL |ZBX_SYNC TABLE|httpstep|httpstepid|ZBX_SYNC -FIELD |httpstepid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |httpstepid |t_id |'0' |NOT NULL |0 FIELD |httptestid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |no |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -104,14 +104,14 @@ FIELD |status_codes |t_varchar(255) |'' |NOT NULL |ZBX_SYNC INDEX |httpstep_1 |httptestid TABLE|httpstepitem|httpstepitemid|ZBX_SYNC -FIELD |httpstepitemid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |httpstepitemid |t_id |'0' |NOT NULL |0 FIELD |httpstepid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC UNIQUE |httpstepitem_1 |httpstepid,itemid TABLE|httptestitem|httptestitemid|ZBX_SYNC -FIELD |httptestitemid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |httptestitemid |t_id |'0' |NOT NULL |0 FIELD |httptestid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -131,7 +131,7 @@ FIELD |nodetype |t_integer |'0' |NOT NULL |0 FIELD |masterid |t_integer |'0' |NOT NULL |0 TABLE|node_cksum||0 -FIELD |nodeid |t_id |'0' |NOT NULL |0 +FIELD |nodeid |t_integer |'0' |NOT NULL |0 FIELD |tablename |t_varchar(64) |'' |NOT NULL |0 FIELD |recordid |t_id |'0' |NOT NULL |0 FIELD |cksumtype |t_integer |'0' |NOT NULL |0 @@ -164,7 +164,7 @@ FIELD |value |t_bigint |'0' |NOT NULL |ZBX_SYNC INDEX |1 |nodeid,id TABLE|services_times|timeid|ZBX_SYNC -FIELD |timeid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |timeid |t_id |'0' |NOT NULL |0 FIELD |serviceid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |ts_from |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -175,7 +175,7 @@ INDEX |times_1 |serviceid,type,ts_from,ts_to -- History tables TABLE|alerts|alertid|ZBX_SYNC -FIELD |alertid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |alertid |t_id |'0' |NOT NULL |0 FIELD |actionid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |triggerid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC @@ -252,7 +252,7 @@ FIELD |value_max |t_double |'0.0000'|NOT NULL |ZBX_SYNC -- Other tables TABLE|acknowledges|acknowledgeid|ZBX_SYNC -FIELD |acknowledgeid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |acknowledgeid |t_id |'0' |NOT NULL |0 FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |eventid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |clock |t_time |'0' |NOT NULL |ZBX_SYNC @@ -262,14 +262,14 @@ INDEX |2 |eventid INDEX |3 |clock TABLE|actions|actionid|ZBX_SYNC -FIELD |actionid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |actionid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |eventsource |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |evaltype |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |status |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|operations|operationid|ZBX_SYNC -FIELD |operationid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |operationid |t_id |'0' |NOT NULL |0 FIELD |actionid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |operationtype |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |object |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -279,7 +279,7 @@ FIELD |longdata |t_blob |'' |NOT NULL |ZBX_SYNC INDEX |1 |actionid TABLE|applications|applicationid|ZBX_SYNC -FIELD |applicationid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |applicationid |t_id |'0' |NOT NULL |0 FIELD |hostid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |name |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |templateid |t_id |'0' |NOT NULL |ZBX_SYNC @@ -297,7 +297,7 @@ INDEX |1 |userid,clock INDEX |2 |clock TABLE|conditions|conditionid|ZBX_SYNC -FIELD |conditionid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |conditionid |t_id |'0' |NOT NULL |0 FIELD |actionid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |conditiontype |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |operator |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -305,7 +305,7 @@ FIELD |value |t_varchar(255) |'' |NOT NULL |ZBX_SYNC INDEX |1 |actionid TABLE|config|configid|ZBX_SYNC -FIELD |configid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |configid |t_id |'0' |NOT NULL |0 FIELD |alert_history |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |event_history |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |refresh_unsupported|t_integer |'0' |NOT NULL |ZBX_SYNC @@ -315,7 +315,7 @@ FIELD |ack_enable |t_integer |'1' |NOT NULL |ZBX_SYNC FIELD |ack_expire |t_integer |'7' |NOT NULL |ZBX_SYNC TABLE|functions|functionid|ZBX_SYNC -FIELD |functionid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |functionid |t_id |'0' |NOT NULL |0 FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |triggerid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |lastvalue |t_varchar(255) | | |0 @@ -325,7 +325,7 @@ INDEX |1 |triggerid INDEX |2 |itemid,function,parameter TABLE|graphs|graphid|ZBX_SYNC -FIELD |graphid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |graphid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(128) |'' |NOT NULL |ZBX_SYNC FIELD |width |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |height |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -341,7 +341,7 @@ FIELD |show_3d |t_integer |'0' |NOT NULL |ZBX_SYNC INDEX |graphs_1 |name TABLE|graphs_items|gitemid|ZBX_SYNC -FIELD |gitemid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |gitemid |t_id |'0' |NOT NULL |0 FIELD |graphid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |drawtype |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -353,7 +353,7 @@ FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |periods_cnt |t_integer |'5' |NOT NULL |ZBX_SYNC TABLE|groups|groupid|ZBX_SYNC -FIELD |groupid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |groupid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC INDEX |1 |name @@ -363,7 +363,7 @@ FIELD |key_ |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |description |t_varchar(255) |'' |NOT NULL |ZBX_SYNC TABLE|hosts|hostid|ZBX_SYNC -FIELD |hostid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |hostid |t_id |'0' |NOT NULL |0 FIELD |host |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |dns |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |useip |t_integer |'1' |NOT NULL |ZBX_SYNC @@ -378,13 +378,13 @@ INDEX |1 |host INDEX |2 |status TABLE|hosts_groups|hostgroupid|ZBX_SYNC -FIELD |hostgroupid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |hostgroupid |t_id |'0' |NOT NULL |0 FIELD |hostid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |groupid |t_id |'0' |NOT NULL |ZBX_SYNC INDEX |groups_1 |hostid,groupid TABLE|hosts_profiles|hostid|ZBX_SYNC -FIELD |hostid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |hostid |t_id |'0' |NOT NULL |0 FIELD |devicetype |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |os |t_varchar(64) |'' |NOT NULL |ZBX_SYNC @@ -398,26 +398,26 @@ FIELD |location |t_blob |'' |NOT NULL |ZBX_SYNC FIELD |notes |t_blob |'' |NOT NULL |ZBX_SYNC TABLE|hosts_templates|hosttemplateid|ZBX_SYNC -FIELD |hosttemplateid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |hosttemplateid |t_id |'0' |NOT NULL |0 FIELD |hostid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |templateid |t_id |'0' |NOT NULL |ZBX_SYNC UNIQUE |1 |hostid,templateid TABLE|housekeeper|housekeeperid|0 -FIELD |housekeeperid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |housekeeperid |t_id |'0' |NOT NULL |0 FIELD |tablename |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |field |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |value |t_id |'0' |NOT NULL |ZBX_SYNC TABLE|images|imageid|ZBX_SYNC -FIELD |imageid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |imageid |t_id |'0' |NOT NULL |0 FIELD |imagetype |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |name |t_varchar(64) |'0' |NOT NULL |ZBX_SYNC FIELD |image |t_image |'' |NOT NULL |ZBX_SYNC INDEX |1 |imagetype,name TABLE|items|itemid|ZBX_SYNC -FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |itemid |t_id |'0' |NOT NULL |0 FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |snmp_community |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |snmp_oid |t_varchar(255) |'' |NOT NULL |ZBX_SYNC @@ -443,10 +443,8 @@ FIELD |snmpv3_securityname|t_varchar(64)|'' |NOT NULL |ZBX_SYNC FIELD |snmpv3_securitylevel|t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |snmpv3_authpassphrase|t_varchar(64)|'' |NOT NULL |ZBX_SYNC FIELD |snmpv3_privpassphrase|t_varchar(64)|'' |NOT NULL |ZBX_SYNC - FIELD |formula |t_varchar(255) |'1' |NOT NULL |ZBX_SYNC FIELD |error |t_varchar(128) |'' |NOT NULL |ZBX_SYNC - FIELD |lastlogsize |t_integer |'0' |NOT NULL |0 FIELD |logtimefmt |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |templateid |t_id |'0' |NOT NULL |ZBX_SYNC @@ -458,21 +456,21 @@ INDEX |2 |nextcheck INDEX |3 |status TABLE|items_applications|itemappid|ZBX_SYNC -FIELD |itemappid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |itemappid |t_id |'0' |NOT NULL |0 FIELD |applicationid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |itemid |t_id |'0' |NOT NULL |ZBX_SYNC INDEX |1 |applicationid,itemid INDEX |2 |itemid TABLE|mappings|mappingid|ZBX_SYNC -FIELD |mappingid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |mappingid |t_id |'0' |NOT NULL |0 FIELD |valuemapid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |value |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |newvalue |t_varchar(64) |'' |NOT NULL |ZBX_SYNC INDEX |1 |valuemapid TABLE|media|mediaid|ZBX_SYNC -FIELD |mediaid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |mediaid |t_id |'0' |NOT NULL |0 FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |mediatypeid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |sendto |t_varchar(100) |'' |NOT NULL |ZBX_SYNC @@ -483,7 +481,7 @@ INDEX |1 |userid INDEX |2 |mediatypeid TABLE|media_type|mediatypeid|ZBX_SYNC -FIELD |mediatypeid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |mediatypeid |t_id |'0' |NOT NULL |0 FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |description |t_varchar(100) |'' |NOT NULL |ZBX_SYNC FIELD |smtp_server |t_varchar(255) |'' |NOT NULL |ZBX_SYNC @@ -495,7 +493,7 @@ FIELD |username |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |passwd |t_varchar(255) |'' |NOT NULL |ZBX_SYNC TABLE|profiles|profileid|ZBX_SYNC -FIELD |profileid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |profileid |t_id |'0' |NOT NULL |0 FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |idx |t_varchar(64) |'' |NOT NULL |ZBX_SYNC FIELD |value |t_varchar(255) |'' |NOT NULL |ZBX_SYNC @@ -503,7 +501,7 @@ FIELD |valuetype |t_integer |0 |NOT NULL |ZBX_SYNC UNIQUE |1 |userid,idx TABLE|rights|rightid|ZBX_SYNC -FIELD |rightid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |rightid |t_id |'0' |NOT NULL |0 FIELD |groupid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |permission |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -511,19 +509,19 @@ FIELD |id |t_id | | |ZBX_SYNC INDEX |1 |groupid TABLE|scripts|scriptid|ZBX_SYNC -FIELD |scriptid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |scriptid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |command |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |host_access |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|screens|screenid|ZBX_SYNC -FIELD |screenid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |screenid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(255) |'Screen'|NOT NULL |ZBX_SYNC FIELD |hsize |t_integer |'1' |NOT NULL |ZBX_SYNC FIELD |vsize |t_integer |'1' |NOT NULL |ZBX_SYNC TABLE|screens_items|screenitemid|ZBX_SYNC -FIELD |screenitemid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |screenitemid |t_id |'0' |NOT NULL |0 FIELD |screenid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |resourcetype |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |resourceid |t_id |'0' |NOT NULL |ZBX_SYNC @@ -540,7 +538,7 @@ FIELD |style |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |url |t_varchar(255) |'' |NOT NULL |ZBX_SYNC TABLE|services|serviceid|ZBX_SYNC -FIELD |serviceid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |serviceid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(128) |'' |NOT NULL |ZBX_SYNC FIELD |status |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |algorithm |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -550,15 +548,15 @@ FIELD |goodsla |t_percentage |'99.9' |NOT NULL |ZBX_SYNC FIELD |sortorder |t_integer |'0' |NOT NULL |ZBX_SYNC TABLE|service_alarms|servicealarmid|0 -FIELD |servicealarmid |t_id |'0' |NOT NULL |ZBX_SYNC -FIELD |serviceid |t_id |'0' |NOT NULL |ZBX_SYNC -FIELD |clock |t_time |'0' |NOT NULL |ZBX_SYNC -FIELD |value |t_integer |'0' |NOT NULL |ZBX_SYNC +FIELD |servicealarmid |t_id |'0' |NOT NULL |0 +FIELD |serviceid |t_id |'0' |NOT NULL |0 +FIELD |clock |t_time |'0' |NOT NULL |0 +FIELD |value |t_integer |'0' |NOT NULL |0 INDEX |1 |serviceid,clock INDEX |2 |clock TABLE|services_links|linkid|ZBX_SYNC -FIELD |linkid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |linkid |t_id |'0' |NOT NULL |0 FIELD |serviceupid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |servicedownid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |soft |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -566,12 +564,12 @@ INDEX |links_1 |servicedownid UNIQUE |links_2 |serviceupid,servicedownid TABLE|sessions|sessionid|0 -FIELD |sessionid |t_varchar(32) |'' |NOT NULL |ZBX_SYNC -FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC -FIELD |lastaccess |t_integer |'0' |NOT NULL |ZBX_SYNC +FIELD |sessionid |t_varchar(32) |'' |NOT NULL |0 +FIELD |userid |t_id |'0' |NOT NULL |0 +FIELD |lastaccess |t_integer |'0' |NOT NULL |0 TABLE|sysmaps_links|linkid|ZBX_SYNC -FIELD |linkid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |linkid |t_id |'0' |NOT NULL |0 FIELD |sysmapid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |selementid1 |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |selementid2 |t_id |'0' |NOT NULL |ZBX_SYNC @@ -582,13 +580,13 @@ FIELD |drawtype_on |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |color_on |t_varchar(32) |'Red' |NOT NULL |ZBX_SYNC TABLE|sysmaps_elements|selementid|ZBX_SYNC -FIELD |selementid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |selementid |t_id |'0' |NOT NULL |0 FIELD |sysmapid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |elementid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |elementtype |t_integer |'0' |NOT NULL |ZBX_SYNC -FIELD |iconid_off |t_bigint |'0' |NOT NULL |ZBX_SYNC -FIELD |iconid_on |t_bigint |'0' |NOT NULL |ZBX_SYNC -FIELD |iconid_unknown |t_bigint |'0' |NOT NULL |ZBX_SYNC +FIELD |iconid_off |t_bigint |'0' |NOT NULL |ZBX_SYNC +FIELD |iconid_on |t_bigint |'0' |NOT NULL |ZBX_SYNC +FIELD |iconid_unknown |t_bigint |'0' |NOT NULL |ZBX_SYNC FIELD |label |t_varchar(128) |'' |NOT NULL |ZBX_SYNC FIELD |label_location |t_integer | |NULL |ZBX_SYNC FIELD |x |t_integer |'0' |NOT NULL |ZBX_SYNC @@ -596,18 +594,18 @@ FIELD |y |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |url |t_varchar(255) |'' |NOT NULL |ZBX_SYNC TABLE|sysmaps|sysmapid|ZBX_SYNC -FIELD |sysmapid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |sysmapid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(128) |'' |NOT NULL |ZBX_SYNC FIELD |width |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |height |t_integer |'0' |NOT NULL |ZBX_SYNC -FIELD |backgroundid |t_bigint |'0' |NOT NULL |ZBX_SYNC +FIELD |backgroundid |t_bigint |'0' |NOT NULL |ZBX_SYNC FIELD |label_type |t_integer |'0' |NOT NULL |ZBX_SYNC FIELD |label_location |t_integer |'0' |NOT NULL |ZBX_SYNC INDEX |1 |name TABLE|triggers|triggerid|ZBX_SYNC -FIELD |triggerid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |triggerid |t_id |'0' |NOT NULL |0 FIELD |expression |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |description |t_varchar(255) |'' |NOT NULL |ZBX_SYNC FIELD |url |t_varchar(255) |'' |NOT NULL |ZBX_SYNC @@ -624,14 +622,14 @@ INDEX |1 |status INDEX |2 |value TABLE|trigger_depends|triggerdepid|ZBX_SYNC -FIELD |triggerdepid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |triggerdepid |t_id |'0' |NOT NULL |0 FIELD |triggerid_down |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |triggerid_up |t_id |'0' |NOT NULL |ZBX_SYNC INDEX |1 |triggerid_down,triggerid_up INDEX |2 |triggerid_up TABLE|users|userid|ZBX_SYNC -FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |userid |t_id |'0' |NOT NULL |0 FIELD |alias |t_varchar(100) |'' |NOT NULL |ZBX_SYNC FIELD |name |t_varchar(100) |'' |NOT NULL |ZBX_SYNC FIELD |surname |t_varchar(100) |'' |NOT NULL |ZBX_SYNC @@ -644,18 +642,18 @@ FIELD |type |t_integer |'0' |NOT NULL |ZBX_SYNC INDEX |1 |alias TABLE|usrgrp|usrgrpid|ZBX_SYNC -FIELD |usrgrpid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |usrgrpid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC INDEX |1 |name TABLE|users_groups|id|ZBX_SYNC -FIELD |id |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |id |t_id |'0' |NOT NULL |0 FIELD |usrgrpid |t_id |'0' |NOT NULL |ZBX_SYNC FIELD |userid |t_id |'0' |NOT NULL |ZBX_SYNC INDEX |1 |usrgrpid,userid TABLE|valuemaps|valuemapid|ZBX_SYNC -FIELD |valuemapid |t_id |'0' |NOT NULL |ZBX_SYNC +FIELD |valuemapid |t_id |'0' |NOT NULL |0 FIELD |name |t_varchar(64) |'' |NOT NULL |ZBX_SYNC INDEX |1 |name diff --git a/include/common.h b/include/common.h index 9bd5a196..4ab855a6 100644 --- a/include/common.h +++ b/include/common.h @@ -159,7 +159,6 @@ #define MAX_BUF_LEN 65000 #define ZBX_DM_DELIMITER '\255' -#define ZBX_CKSUM_DELIMITER ',' /* Item types */ typedef enum @@ -545,7 +544,7 @@ int get_param(const char *param, int num, char *buf, int maxlen); int num_param(const char *param); int calculate_item_nextcheck(zbx_uint64_t itemid, int item_type, int delay, char *delay_flex, time_t now); int check_time_period(const char *period, time_t now); -void zbx_binary2hex(const u_char *input, int ilen, char **output, int *olen); +int zbx_binary2hex(const u_char *input, int ilen, char **output, int *olen); int zbx_hex2binary(char *io); void zbx_hex2octal(const char *input, char **output, int *olen); #ifdef HAVE_POSTGRESQL diff --git a/src/libs/zbxcommon/str.c b/src/libs/zbxcommon/str.c index 573fe066..4b7738d3 100644 --- a/src/libs/zbxcommon/str.c +++ b/src/libs/zbxcommon/str.c @@ -1182,7 +1182,7 @@ u_char zbx_hex2num(char c) * Comments: * * * ******************************************************************************/ -void zbx_binary2hex(const u_char *input, int ilen, char **output, int *olen) +int zbx_binary2hex(const u_char *input, int ilen, char **output, int *olen) { const u_char *i = input; char *o; @@ -1193,19 +1193,20 @@ void zbx_binary2hex(const u_char *input, int ilen, char **output, int *olen) assert(*output); assert(olen); - if(*olen < len) - { + if (*olen < len) { *olen = len; *output = zbx_realloc(*output, *olen); } o = *output; - while(i - input < ilen) { + while (i - input < ilen) { *o++ = zbx_num2hex( (*i >> 4) & 0xf ); *o++ = zbx_num2hex( *i & 0xf ); i++; } *o = '\0'; + + return len - 1; } /****************************************************************************** diff --git a/src/zabbix_agent/logfiles.c b/src/zabbix_agent/logfiles.c index ec3b49df..b78f42ca 100644 --- a/src/zabbix_agent/logfiles.c +++ b/src/zabbix_agent/logfiles.c @@ -57,7 +57,9 @@ int process_log( assert(lastlogsize); assert(value); - zabbix_log( LOG_LEVEL_DEBUG, "In process log (%s,%li)", filename, *lastlogsize); + zabbix_log( LOG_LEVEL_DEBUG, "In process log (%s,%li)", + filename, + *lastlogsize); /* Handling of file shrinking */ if( 0 != stat(filename,&buf) ) diff --git a/src/zabbix_server/nodewatcher/history.c b/src/zabbix_server/nodewatcher/history.c index 4f425268..c9fc79d1 100644 --- a/src/zabbix_server/nodewatcher/history.c +++ b/src/zabbix_server/nodewatcher/history.c @@ -24,9 +24,10 @@ #include "log.h" #include "zlog.h" +#include "history.h" +#include "nodewatcher.h" #include "nodecomms.h" #include "nodesender.h" -#include "history.h" /****************************************************************************** * * @@ -51,11 +52,12 @@ static int process_node_history_log(int nodeid, int master_nodeid) DB_RESULT result; DB_ROW row; char sql[MAX_STRING_LEN]; - int ids_found = 0, found = 0; - int offset = 0; + int ids_found = 0, found = 0, len; char *data = NULL, *hex = NULL; - int allocated = 1024*1024, hex_allocated = 1024; - zbx_uint64_t sync_lastid = 0, id, len; + int data_allocated = 1024*1024, data_offset = 0; + int hex_allocated = 1024; + zbx_uint64_t sync_lastid = 0, id; + int start = time(NULL); zabbix_log( LOG_LEVEL_DEBUG, "In process_node_history_log(nodeid:%d, master_nodeid:%d)", nodeid, @@ -66,7 +68,7 @@ static int process_node_history_log(int nodeid, int master_nodeid) return SUCCEED; - data = zbx_malloc(data, allocated); + data = zbx_malloc(data, data_allocated); hex = zbx_malloc(hex, hex_allocated); result = DBselect("select nextid from ids where nodeid=%d and table_name='%s' and field_name='%s'", @@ -74,44 +76,42 @@ static int process_node_history_log(int nodeid, int master_nodeid) ids_table_name, ids_field_name); - if((row=DBfetch(result))) - { + if ((row = DBfetch(result))) { ZBX_STR2UINT64(sync_lastid,row[0]) ids_found = 1; } DBfree_result(result); - zbx_snprintf_alloc(&data, &allocated, &offset, 64, "History%c%d%c%d", + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 64, "History%c%d%c%d", ZBX_DM_DELIMITER, CONFIG_NODEID, ZBX_DM_DELIMITER, nodeid); - zbx_snprintf(sql,sizeof(sql),"select id,itemid,clock,timestamp,source,severity,value,length(value) from history_log where id>"ZBX_FS_UI64" and "ZBX_COND_NODEID" order by id", + zbx_snprintf(sql,sizeof(sql),"select id,itemid,clock,timestamp,source,severity,value,length(value) " + "from history_log where id>"ZBX_FS_UI64" and "ZBX_COND_NODEID" order by id", sync_lastid, ZBX_NODE("id", nodeid)); result = DBselectN(sql, 10000); - while((row=DBfetch(result))) - { + while ((row = DBfetch(result))) { ZBX_STR2UINT64(id,row[0]) - found = 1; len = atoi(row[7]); - zbx_binary2hex((u_char *)row[6], len, &hex, &hex_allocated); - - zbx_snprintf_alloc(&data, &allocated, &offset, len * 2 + 256, "\n%d%c%s%c%s%c%s%c%s%c%s%c%s%c%s", - ZBX_TABLE_HISTORY_LOG, ZBX_DM_DELIMITER, - row[1], ZBX_DM_DELIMITER, /* itemid */ - row[2], ZBX_DM_DELIMITER, /* clock */ - row[0], ZBX_DM_DELIMITER, /* id */ - row[3], ZBX_DM_DELIMITER, /* timestamp */ - row[4], ZBX_DM_DELIMITER, /* source */ - row[5], ZBX_DM_DELIMITER, /* severity */ - hex); /* value */ + len = zbx_binary2hex((u_char *)row[6], len, &hex, &hex_allocated); + + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, len + 256, "\n%d%c%s%c%s%c%s%c%s%c%s%c%s%c%s", + ZBX_TABLE_HISTORY_LOG, ZBX_DM_DELIMITER, + row[1], ZBX_DM_DELIMITER, /* itemid */ + row[2], ZBX_DM_DELIMITER, /* clock */ + row[0], ZBX_DM_DELIMITER, /* id */ + row[3], ZBX_DM_DELIMITER, /* timestamp */ + row[4], ZBX_DM_DELIMITER, /* source */ + row[5], ZBX_DM_DELIMITER, /* severity */ + hex); /* value */ + found = 1; } - if(found == 1) - { + if (found == 1) { if(send_to_node("new history_log", master_nodeid, nodeid, data) == SUCCEED) { if(ids_found == 1) @@ -132,15 +132,15 @@ static int process_node_history_log(int nodeid, int master_nodeid) } } else - { zabbix_log( LOG_LEVEL_DEBUG, "process_node_history_log() FAIL"); - } } DBfree_result(result); - zbx_free(data); zbx_free(hex); + zabbix_log( LOG_LEVEL_DEBUG, "Spent %d seconds in process_node_history_log", + time(NULL)-start); + return SUCCEED; } @@ -164,12 +164,12 @@ static int process_node_history_str(int nodeid, int master_nodeid) { DB_RESULT result; DB_ROW row; - char *data = NULL; + char *data = NULL, *hex = NULL; char sql[MAX_STRING_LEN]; - int found = 0; - int offset = 0; - int allocated = 1024*1024; - + int found = 0, len; + int data_allocated = 1024*1024, data_offset = 0;; + int hex_allocated = 512; + int start = time(NULL); zbx_uint64_t id; zabbix_log( LOG_LEVEL_DEBUG, "In process_node_history_str(nodeid:%d, master_nodeid:%d", @@ -177,36 +177,35 @@ static int process_node_history_str(int nodeid, int master_nodeid) master_nodeid); /* Begin work */ - data = zbx_malloc(data, allocated); - memset(data,0,allocated); + data = zbx_malloc(data, data_allocated); + hex = zbx_malloc(hex, hex_allocated); - zbx_snprintf_alloc(&data, &allocated, &offset, 128, "History%c%d%c%d", + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "History%c%d%c%d", ZBX_DM_DELIMITER, CONFIG_NODEID, ZBX_DM_DELIMITER, nodeid); - zbx_snprintf(sql,sizeof(sql),"select id,itemid,clock,value from history_str_sync where nodeid=%d order by id", + zbx_snprintf(sql,sizeof(sql),"select id,itemid,clock,value from history_str_sync " + "where nodeid=%d order by id", nodeid); result = DBselectN(sql, 10000); - while((row=DBfetch(result))) - { + while ((row = DBfetch(result))) { ZBX_STR2UINT64(id,row[0]) + + len = strlen(row[3]); + len = zbx_binary2hex((u_char *)row[3], len, &hex, &hex_allocated); + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, len + 128, "\n%d%c%s%c%s%c%s", + ZBX_TABLE_HISTORY_STR, ZBX_DM_DELIMITER, + row[1], ZBX_DM_DELIMITER, + row[2], ZBX_DM_DELIMITER, + hex); found = 1; - zbx_snprintf_alloc(&data, &allocated, &offset, 1024, "\n%d%c%s%c%s%c%s", - ZBX_TABLE_HISTORY_STR, - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - row[2], - ZBX_DM_DELIMITER, - row[3]); } - if(found == 1) - { + if (found == 1) { /* Do not send history for current node if CONFIG_NODE_NOHISTORY is set */ - if( ((CONFIG_NODE_NOHISTORY !=0) && (CONFIG_NODEID == nodeid)) || + if (((CONFIG_NODE_NOHISTORY !=0) && (CONFIG_NODEID == nodeid)) || send_to_node("new history_str", master_nodeid, nodeid, data) == SUCCEED) { DBexecute("delete from history_str_sync where nodeid=%d and id<=" ZBX_FS_UI64, @@ -214,12 +213,14 @@ static int process_node_history_str(int nodeid, int master_nodeid) id); } else - { zabbix_log( LOG_LEVEL_DEBUG, "process_node_history_str() FAIL"); - } } DBfree_result(result); zbx_free(data); + zbx_free(hex); + + zabbix_log( LOG_LEVEL_DEBUG, "Spent %d seconds in process_node_history_str", + time(NULL)-start); return SUCCEED; } @@ -247,24 +248,17 @@ static int process_node_history_uint(int nodeid, int master_nodeid) char *data= NULL; char sql[MAX_STRING_LEN]; int found = 0; - int offset = 0; - int allocated = 1024*1024; - - int start, end; - + int data_allocated = 1024*1024, data_offset = 0; + int start = time(NULL); zbx_uint64_t id; zabbix_log( LOG_LEVEL_DEBUG, "In process_node_history_uint(nodeid:%d, master_nodeid:%d)", nodeid, master_nodeid); /* Begin work */ + data = zbx_malloc(data, data_allocated); - start = time(NULL); - - data = zbx_malloc(data, allocated); - memset(data,0,allocated); - - zbx_snprintf_alloc(&data, &allocated, &offset, 128, "History%c%d%c%d", + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "History%c%d%c%d", ZBX_DM_DELIMITER, CONFIG_NODEID, ZBX_DM_DELIMITER, @@ -278,7 +272,7 @@ static int process_node_history_uint(int nodeid, int master_nodeid) { ZBX_STR2UINT64(id,row[0]) found = 1; - zbx_snprintf_alloc(&data, &allocated, &offset, 128, "\n%d%c%s%c%s%c%s", + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n%d%c%s%c%s%c%s", ZBX_TABLE_HISTORY_UINT, ZBX_DM_DELIMITER, row[1], @@ -296,19 +290,14 @@ static int process_node_history_uint(int nodeid, int master_nodeid) DBexecute("delete from history_uint_sync where nodeid=%d and id<=" ZBX_FS_UI64, nodeid, id); - } - else - { + } else zabbix_log( LOG_LEVEL_DEBUG, "process_node_history_uint() FAIL"); - } } DBfree_result(result); zbx_free(data); - end = time(NULL); - zabbix_log( LOG_LEVEL_DEBUG, "Spent %d seconds in process_node_history_uint", - end-start); + time(NULL)-start); return SUCCEED; } @@ -336,23 +325,18 @@ static int process_node_history(int nodeid, int master_nodeid) char *data = NULL; char sql[MAX_STRING_LEN]; int found = 0; - int offset = 0; - int allocated = 1024*1024; - - int start, end; - + int data_allocated = 512*1024, data_offset = 0; + int start = time(NULL); zbx_uint64_t id; - zabbix_log( LOG_LEVEL_DEBUG, "In process_node_history(nodeid:%d, master_nodeid:%d", + zabbix_log(LOG_LEVEL_DEBUG, "In process_node_history(nodeid:%d, master_nodeid:%d", nodeid, master_nodeid); - /* Begin work */ - start = time(NULL); - data = zbx_malloc(data, allocated); - memset(data,0,allocated); + /* Begin work */ + data = zbx_malloc(data, data_allocated); - zbx_snprintf_alloc(&data, &allocated, &offset, 128, "History%c%d%c%d", + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "History%c%d%c%d", ZBX_DM_DELIMITER, CONFIG_NODEID, ZBX_DM_DELIMITER, @@ -362,21 +346,19 @@ static int process_node_history(int nodeid, int master_nodeid) nodeid); result = DBselectN(sql, 10000); - while((row=DBfetch(result))) - { + while ((row = DBfetch(result))) { ZBX_STR2UINT64(id,row[0]) found = 1; - zbx_snprintf_alloc(&data, &allocated, &offset, 128, "\n%d%c%s%c%s%c%s", - ZBX_TABLE_HISTORY, - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - row[2], - ZBX_DM_DELIMITER, - row[3]); + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n%d%c%s%c%s%c%s", + ZBX_TABLE_HISTORY, + ZBX_DM_DELIMITER, + row[1], + ZBX_DM_DELIMITER, + row[2], + ZBX_DM_DELIMITER, + row[3]); } - if(found == 1) - { + if (found == 1) { zabbix_log( LOG_LEVEL_DEBUG, "Sending [%s]", data); /* Do not send history for current node if CONFIG_NODE_NOHISTORY is set */ @@ -386,19 +368,14 @@ static int process_node_history(int nodeid, int master_nodeid) DBexecute("delete from history_sync where nodeid=%d and id<=" ZBX_FS_UI64, nodeid, id); - } - else - { + } else zabbix_log( LOG_LEVEL_DEBUG, "process_node_history() FAIL"); - } } DBfree_result(result); zbx_free(data); - end = time(NULL); - zabbix_log( LOG_LEVEL_DEBUG, "Spent %d seconds in process_node_history", - end-start); + time(NULL)-start); return SUCCEED; } @@ -450,26 +427,25 @@ void main_historysender() { DB_RESULT result; DB_ROW row; - int nodeid; - int master_nodeid; + int master_nodeid, nodeid; zabbix_log( LOG_LEVEL_DEBUG, "In main_historysender()"); DBbegin(); master_nodeid = CONFIG_MASTER_NODEID; + if (0 == master_nodeid) + return; - if(master_nodeid) - { - result = DBselect("select nodeid from nodes where nodeid<>%d", master_nodeid); + result = DBselect("select nodeid from nodes"); + while ((row = DBfetch(result))) { + nodeid = atoi(row[0]); + if (SUCCEED == is_master_node(CONFIG_NODEID, nodeid)) + continue; - while((row = DBfetch(result))) - { - nodeid=atoi(row[0]); - process_node(nodeid, master_nodeid); - } - DBfree_result(result); + process_node(nodeid, master_nodeid); } + DBfree_result(result); DBcommit(); } diff --git a/src/zabbix_server/nodewatcher/history.h b/src/zabbix_server/nodewatcher/history.h index 69aaf4e4..cf064cc3 100644 --- a/src/zabbix_server/nodewatcher/history.h +++ b/src/zabbix_server/nodewatcher/history.h @@ -20,8 +20,6 @@ #ifndef ZABBIX_NODEWATCHER_HISTORY_H #define ZABBIX_NODEWATCHER_HISTORY_H -extern int CONFIG_NODE_NOHISTORY; - void main_historysender(); #endif diff --git a/src/zabbix_server/nodewatcher/nodecomms.h b/src/zabbix_server/nodewatcher/nodecomms.h index 793d8251..d4b0da16 100644 --- a/src/zabbix_server/nodewatcher/nodecomms.h +++ b/src/zabbix_server/nodewatcher/nodecomms.h @@ -22,9 +22,6 @@ #include "comms.h" -#define ZBX_NODE_MASTER 0 -#define ZBX_NODE_SLAVE 1 - int send_to_node(char *name,int dest_nodeid, int nodeid, char *data); int connect_to_node(int nodeid, zbx_sock_t *sock); diff --git a/src/zabbix_server/nodewatcher/nodesender.c b/src/zabbix_server/nodewatcher/nodesender.c index 2f7532fc..08834ce1 100644 --- a/src/zabbix_server/nodewatcher/nodesender.c +++ b/src/zabbix_server/nodewatcher/nodesender.c @@ -23,19 +23,142 @@ #include "db.h" #include "log.h" #include "zlog.h" - #include "dbsync.h" -#include "nodecomms.h" + #include "nodesender.h" +#include "nodewatcher.h" +#include "nodecomms.h" +#include "../trapper/nodesync.h" -#define ZBX_NODE_MASTER 0 -#define ZBX_NODE_SLAVE 1 /****************************************************************************** * * - * Function: get_slave_node * + * Function: calculate_checksums * * * - * Purpose: * + * Purpose: calculate check sums of configuration data * + * * + * Parameters: * + * * + * Return value: SUCCESS - calculated succesfully * + * FAIL - an error occured * + * * + * Author: Alexei Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id) +{ + char *sql = NULL; + int sql_allocated = 16*1024, sql_offset = 0; + int t, f, res = SUCCEED; + + zabbix_log(LOG_LEVEL_DEBUG, "In calculate_checksums"); + + sql = zbx_malloc(sql, sql_allocated); + + for (t = 0; tables[t].table != 0; t++) { + /* Do not sync some of tables */ + if ((tables[t].flags & ZBX_SYNC) == 0) + continue; + + if (NULL != tablename && 0 != strcmp(tablename, tables[t].table)) + continue; + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, +#ifdef HAVE_MYSQL + "%s select %d,'%s',%s,%d,concat_ws(',',", +#else + "%s select %d,'%s',%s,%d,", +#endif + sql_offset > 0 ? "union all" : "insert into node_cksum (nodeid,tablename,recordid,cksumtype,cksum)", + nodeid, + tables[t].table, + tables[t].recid, + NODE_CKSUM_TYPE_NEW); + + for (f = 0; tables[t].fields[f].name != 0; f ++) { + if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) + continue; + + if (tables[t].fields[f].flags & ZBX_NOTNULL) { + switch ( tables[t].fields[f].type ) { + case ZBX_TYPE_ID : + case ZBX_TYPE_INT : + case ZBX_TYPE_UINT : + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "%s", + tables[t].fields[f].name); + break; + default : + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "md5(%s)", + tables[t].fields[f].name); + break; + } + } else { + switch ( tables[t].fields[f].type ) { + case ZBX_TYPE_ID : + case ZBX_TYPE_INT : + case ZBX_TYPE_UINT : + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "case when %s is null then 'NULL' else %1$s end", + tables[t].fields[f].name); + break; + default : + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "case when %s is null then 'NULL' else md5(%1$s) end", + tables[t].fields[f].name); + break; + } + } + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, +#ifdef HAVE_MYSQL + "," +#else + "||','||" +#endif + ); + } + + /* remove last delimiter */ + if (f > 0) { +#ifdef HAVE_MYSQL + sql_offset --; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, ")"); +#else + sql_offset -= 7; +#endif + } + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + " from %s where"ZBX_COND_NODEID, + tables[t].table, + ZBX_NODE(tables[t].recid,nodeid)); + + if (0 != id) { + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "and %s="ZBX_FS_UI64, + tables[t].recid, + id); + } + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "\n"); + } + if (SUCCEED == res && DBexecute("delete from node_cksum where nodeid=%d and cksumtype=%d", + nodeid, + NODE_CKSUM_TYPE_NEW) < ZBX_DB_OK) + res = FAIL; + if (SUCCEED == res && DBexecute("%s", sql) < ZBX_DB_OK) + res = FAIL; + + return res; +} + +/****************************************************************************** + * * + * Function: send_config_data * + * * + * Purpose: send configuration changes to required node * * * * Parameters: * * * @@ -47,77 +170,497 @@ * Comments: * * * ******************************************************************************/ -static int get_slave_node(int nodeid, int synked_nodeid) +char *get_config_data(int nodeid, int dest_nodetype) { DB_RESULT result; + DB_RESULT result2; DB_ROW row; - int master_nodeid; + DB_ROW row2; - zabbix_log( LOG_LEVEL_DEBUG, "In get_slave_node(%d)", + char *data = NULL, *hex = NULL, *sql = NULL, c, sync[129], *s, *r[2], *d[2]; + int data_offset=0, sql_offset = 0; + int data_allocated=1024, hex_allocated=1024, sql_allocated=8*1024; + int t, f, j, rowlen; + + zabbix_log( LOG_LEVEL_DEBUG, "In get_config_data(node:%d,dest_nodetype:%s)", + nodeid, + dest_nodetype == ZBX_NODE_MASTER ? "MASTER" : "SLAVE"); + + data = zbx_malloc(data, data_allocated); + hex = zbx_malloc(hex, hex_allocated); + sql = zbx_malloc(sql, sql_allocated); + c = '1'; + + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "Data%c%d%c%d\n", + ZBX_DM_DELIMITER, + CONFIG_NODEID, + ZBX_DM_DELIMITER, nodeid); - result = DBselect("select masterid from nodes where nodeid=%d", - synked_nodeid); - if (NULL != (row = DBfetch(result))) - master_nodeid = atoi(row[0]); - else - master_nodeid = 0; + /* Find updated records */ + result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " + "from node_cksum curr, node_cksum prev " + "where curr.nodeid=%1$d and prev.nodeid=%1$d and " + "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " + "curr.cksumtype=%3$d and prev.cksumtype=%2$d " + /*" and curr.tablename='hosts' "*/ + "union all " + /* Find new records */ + "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,curr.sync " + "from node_cksum curr left join node_cksum prev " + "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " + "prev.recordid=curr.recordid and prev.cksumtype=%2$d " + "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null " + /*" and curr.tablename='hosts' "*/ + "union all " + /* Find deleted records */ + "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " + "from node_cksum prev left join node_cksum curr " + "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " + "curr.recordid=prev.recordid and curr.cksumtype=%3$d " + "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null" + /*" and prev.tablename='hosts' "*/, + nodeid, + NODE_CKSUM_TYPE_OLD, /* prev */ + NODE_CKSUM_TYPE_NEW); /* curr */ + + while (NULL != (row = DBfetch(result))) { + for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) + ; + + /* Found table */ + if (tables[t].table == 0) { + zabbix_log( LOG_LEVEL_WARNING, "Cannot find table [%s]", + row[0]); + continue; + } + + if (DBis_null(row[4]) == FAIL) + strcpy(sync, row[4]); + else + memset(sync, ' ', sizeof(sync)); + s = sync; + + /* Special (simpler) processing for operation DELETE */ + if (DBis_null(row[2]) == FAIL && DBis_null(row[3]) == SUCCEED && + ((dest_nodetype == ZBX_NODE_SLAVE && *s != c) || + (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c))) { + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d\n", + row[0], + ZBX_DM_DELIMITER, + row[1], + ZBX_DM_DELIMITER, + NODE_CONFIGLOG_OP_DELETE); + continue; + } + + r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; + r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; + f = 0; + sql_offset = 0; + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "select "); + do { + while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) + f++; + + d[0] = NULL; + d[1] = NULL; + if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) + *d[0] = '\0'; + if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) + *d[1] = '\0'; + + if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || + (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "%s,length(%1$s),", + tables[t].fields[f].name); + } + s += 2; + f++; + + if (d[0] != NULL) { + *d[0] = ','; + r[0] = d[0] + 1; + } else + r[0] = NULL; + if (d[1] != NULL) { + *d[1] = ','; + r[1] = d[1] + 1; + } else + r[1] = NULL; + } while (d[0] != NULL || d[1] != NULL); + + if (sql[sql_offset-1] != ',') + continue; + + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, " from %s where %s=%s", + row[0], + tables[t].recid, + row[1]); + + result2 = DBselect("%s", sql); + if (NULL == (row2=DBfetch(result2))) + goto out; + + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d", + row[0], + ZBX_DM_DELIMITER, + row[1], + ZBX_DM_DELIMITER, + NODE_CONFIGLOG_OP_UPDATE); + + r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; + r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; + s = sync; + f = 0; + j = 0; + + do { + while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) + f++; + + d[0] = NULL; + d[1] = NULL; + if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) + *d[0] = '\0'; + if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) + *d[1] = '\0'; + + if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || + (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { + + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%c%s%c%d%c", + ZBX_DM_DELIMITER, + tables[t].fields[f].name, + ZBX_DM_DELIMITER, + tables[t].fields[f].type, + ZBX_DM_DELIMITER); + + /* Fieldname, type, value */ + if (DBis_null(row2[j*2]) == SUCCEED) { + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "NULL"); + } else if(tables[t].fields[f].type == ZBX_TYPE_INT || + tables[t].fields[f].type == ZBX_TYPE_UINT || + tables[t].fields[f].type == ZBX_TYPE_ID || + tables[t].fields[f].type == ZBX_TYPE_FLOAT) { + + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s", row2[j*2]); + } else { + rowlen = atoi(row2[j*2+1]); + zbx_binary2hex((u_char *)row2[j*2], rowlen, &hex, &hex_allocated); + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, strlen(hex)+128, "%s", hex); +/*zabbix_log(LOG_LEVEL_CRIT, "----- [field:%s][type:%d][row:%s][hex:%s]",tables[t].fields[f].name,tables[t].fields[f].type,row2[j*2],hex);*/ + } + j++; + } + s += 2; + f++; + + if (d[0] != NULL) { + *d[0] = ','; + r[0] = d[0] + 1; + } else + r[0] = NULL; + if (d[1] != NULL) { + *d[1] = ','; + r[1] = d[1] + 1; + } else + r[1] = NULL; + } while (d[0] != NULL || d[1] != NULL); + zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n"); +out: + DBfree_result(result2); + } DBfree_result(result); - if (master_nodeid == 0) - return 0; - if (master_nodeid == nodeid) - return synked_nodeid; - return get_slave_node(nodeid, master_nodeid); + zbx_free(hex); + zbx_free(sql); + + return data; } /****************************************************************************** * * - * Function: main_nodesender * + * Function: update_checksums * * * - * Purpose: periodically sends config changes and history to related nodes * + * Purpose: overwrite old checksums with new ones * * * * Parameters: * * * - * Return value: * + * Return value: SUCCESS - calculated succesfully * + * FAIL - an error occured * * * * Author: Alexei Vladishev * * * - * Comments: never returns * + * Comments: * * * ******************************************************************************/ -/*void main_nodesender(int synked_nodeid, int *synked_slave, int *synked_master) +int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields) { + char *r[2], *d[2], sync[129], *s; + char c, sql[2][256]; + char cksum[32*64+32], *ck; DB_RESULT result; DB_ROW row; - int nodeid, slave_nodeid, master_nodeid; - char *data; + int t, f; - zabbix_log(LOG_LEVEL_DEBUG, "In main_nodesender()"); + zabbix_log(LOG_LEVEL_DEBUG, "In update_checksums"); - *synked_slave = FAIL; - *synked_master = FAIL; + c = synked == SUCCEED ? '1' : ' '; - result = DBselect("select nodeid from nodes where nodetype=%d", - ZBX_NODE_TYPE_LOCAL); + if (NULL != tablename) { + zbx_snprintf(sql[0], sizeof(sql[0]), " and curr.tablename='%s' and curr.recordid="ZBX_FS_UI64, + tablename, id); + zbx_snprintf(sql[1], sizeof(sql[1]), " and prev.tablename='%s' and prev.recordid="ZBX_FS_UI64, + tablename, id); + } else { + *sql[0] = '\0'; + *sql[1] = '\0'; + } - if (NULL != (row = DBfetch(result))) { - nodeid = atoi(row[0]); - if (CONFIG_NODEID != nodeid) { - zabbix_log(LOG_LEVEL_WARNING, "NodeID does not match configuration settings." - " Processing of the node is disabled."); - } else { - slave_nodeid = get_slave_node(nodeid, synked_nodeid); - master_nodeid = CONFIG_MASTER_NODEID; - if (0 != slave_nodeid && NULL != (data = get_config_data(synked_nodeid, ZBX_NODE_SLAVE))) { - *synked_slave = send_to_node("configuration changes", slave_nodeid, synked_nodeid, data); - zbx_free(data); + /* Find updated records */ + result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " + "from node_cksum curr, node_cksum prev " + "where curr.nodeid=%1$d and prev.nodeid=%1$d and " + "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " + "curr.cksumtype=%3$d and prev.cksumtype=%2$d%4$s " + "union all " + /* Find new records */ + "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,NULL " + "from node_cksum curr left join node_cksum prev " + "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " + "prev.recordid=curr.recordid and prev.cksumtype=%2$d " + "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null%4$s " + "union all " + /* Find deleted records */ + "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " + "from node_cksum prev left join node_cksum curr " + "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " + "curr.recordid=prev.recordid and curr.cksumtype=%3$d " + "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null%5$s", + nodeid, + NODE_CKSUM_TYPE_OLD, /* prev */ + NODE_CKSUM_TYPE_NEW, /* curr */ + sql[0], + sql[1]); + + while (NULL != (row = DBfetch(result))) { + for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) + ; + + /* Found table */ + if (tables[t].table == 0) { + zabbix_log(LOG_LEVEL_WARNING, "Cannot find table [%s]", + row[0]); + continue; + } + + if (DBis_null(row[4]) == FAIL) + strcpy(sync, row[4]); + else + memset(sync, ' ', sizeof(sync)); + s = sync; + ck = cksum; + *ck = '\0'; + + /* Special (simpler) processing for operation DELETE */ + if (DBis_null(row[3]) == SUCCEED) { + if (*(s+2) != '\0') { + *s = ' '; + *(s+1) = ' '; } - if (0 != master_nodeid && NULL != (data = get_config_data(synked_nodeid, ZBX_NODE_MASTER))) { - *synked_master = send_to_node("configuration changes", master_nodeid, synked_nodeid, data); - zbx_free(data); + if (synked == SUCCEED) { + if (synked_nodetype == ZBX_NODE_SLAVE) + *s = c; + else if (synked_nodetype == ZBX_NODE_MASTER) + *(s+1) = c; + } + s += 2; + } else { + r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; + r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; + f = 0; + + do { + while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) + f++; + + d[0] = NULL; + d[1] = NULL; + if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) + *d[0] = '\0'; + if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) + *d[1] = '\0'; + + if (NULL == tablename || SUCCEED == str_in_list(fields, tables[t].fields[f].name, ',')) { + ck += zbx_snprintf(ck, 64, "%s,", NULL != r[1] ? r[1] : r[0]); + + if (r[0] == NULL || r[1] == NULL || strcmp(r[0], r[1]) != 0) { + if (synked_nodetype == ZBX_NODE_SLAVE) { + *s = c; + *(s+1) = ' '; + } else if (synked_nodetype == ZBX_NODE_MASTER) { + *s = ' '; + *(s+1) = c; + } + } else { + if (synked == SUCCEED) { + if (synked_nodetype == ZBX_NODE_SLAVE) + *s = c; + else if (synked_nodetype == ZBX_NODE_MASTER) + *(s+1) = c; + } + } + } else + ck += zbx_snprintf(ck, 64, "%s,", NULL != r[0] ? r[0] : ""); + s += 2; + f++; + + if (d[0] != NULL) { + *d[0] = ','; + r[0] = d[0] + 1; + } else + r[0] = NULL; + if (d[1] != NULL) { + *d[1] = ','; + r[1] = d[1] + 1; + } else + r[1] = NULL; + } while (d[0] != NULL || d[1] != NULL); + } + *s = '\0'; + *--ck = '\0'; + + if (DBis_null(row[2]) == SUCCEED || DBis_null(row[3]) == SUCCEED || + strcmp(row[4], sync) != 0 || strcmp(row[2], row[3]) != 0) + { + DBexecute("update node_cksum set cksumtype=%d,cksum=\'%s\',sync=\'%s\' " + "where nodeid=%d and tablename=\'%s\' and recordid=%s and cksumtype=%d", + NODE_CKSUM_TYPE_OLD, + cksum, + sync, + nodeid, + row[0], + row[1], + DBis_null(row[2]) == SUCCEED ? NODE_CKSUM_TYPE_NEW : NODE_CKSUM_TYPE_OLD); + } + } + DBfree_result(result); + + return SUCCEED; +} + +/****************************************************************************** + * * + * Function: node_sync_lock * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void node_sync_lock(int nodeid) +{ + zbx_mutex_lock(&node_sync_access); +} + +/****************************************************************************** + * * + * Function: node_sync_unlock * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void node_sync_unlock(int nodeid) +{ + zbx_mutex_unlock(&node_sync_access); +} + +/****************************************************************************** + * * + * Function: process_nodes * + * * + * Purpose: calculates checks sum of config data * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: never returns * + * * + ******************************************************************************/ +void process_nodes() +{ + DB_RESULT result; + DB_ROW row; + int nodeid; + int master_nodeid; + char *data, *answer; + zbx_sock_t sock; + int res; +/* int now = time(NULL);*/ + int sender_nodeid; + + master_nodeid = CONFIG_MASTER_NODEID; + if (0 == master_nodeid) + return; + + result = DBselect("select nodeid from nodes"); + while (NULL != (row=DBfetch(result))) { + nodeid = atoi(row[0]); + if (SUCCEED == is_master_node(CONFIG_NODEID, nodeid)) + continue; + + node_sync_lock(nodeid); + +/* DBbegin();*/ + + res = calculate_checksums(nodeid, NULL, 0); + if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_MASTER))) { + zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Sending configuration changes to master node %d for node %d datalen %d", + CONFIG_NODEID, + master_nodeid, + nodeid, + strlen(data)); + if (SUCCEED == (res = connect_to_node(master_nodeid, &sock))) { + if (SUCCEED == res) + res = send_data_to_node(master_nodeid, &sock, data); + if (SUCCEED == res) + res = recv_data_from_node(master_nodeid, &sock, &answer); + if (SUCCEED == res && 0 == strncmp(answer, "Data", 4)) { + res = update_checksums(nodeid, ZBX_NODE_MASTER, SUCCEED, NULL, 0, NULL); + if (SUCCEED == res) + res = node_sync(answer, &sender_nodeid, &nodeid); + send_data_to_node(master_nodeid, &sock, SUCCEED == res ? "OK" : "FAIL"); + } + disconnect_node(&sock); } + zbx_free(data); } + +/* DBcommit();*/ + + node_sync_unlock(nodeid); } DBfree_result(result); -}*/ + +/* zabbix_log(LOG_LEVEL_CRIT, "<-----> process_nodes [Selected records in %d seconds]", time(NULL)-now);*/ +} diff --git a/src/zabbix_server/nodewatcher/nodesender.h b/src/zabbix_server/nodewatcher/nodesender.h index 553c9693..7f64e5da 100644 --- a/src/zabbix_server/nodewatcher/nodesender.h +++ b/src/zabbix_server/nodewatcher/nodesender.h @@ -20,6 +20,18 @@ #ifndef ZABBIX_NODESENDER_H #define ZABBIX_NODESENDER_H -void main_nodesender(int nodeid, int *synked_slave, int *synked_master); +#include "mutexs.h" + +#define ZBX_NODE_MASTER 0 +#define ZBX_NODE_SLAVE 1 + +extern ZBX_MUTEX node_sync_access; + +int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id); +char *get_config_data(int nodeid, int dest_nodetype); +int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields); +void node_sync_lock(int nodeid); +void node_sync_unlock(int nodeid); +void process_nodes(); #endif diff --git a/src/zabbix_server/nodewatcher/nodewatcher.c b/src/zabbix_server/nodewatcher/nodewatcher.c index a4ecc6c5..661dba5a 100644 --- a/src/zabbix_server/nodewatcher/nodewatcher.c +++ b/src/zabbix_server/nodewatcher/nodewatcher.c @@ -23,646 +23,46 @@ #include "log.h" #include "zlog.h" -#include "dbsync.h" +#include "nodewatcher.h" +#include "nodesender.h" #include "events.h" #include "history.h" -#include "comms.h" -#include "nodecomms.h" -#include "nodewatcher.h" -#include "../trapper/nodesync.h" /****************************************************************************** * * - * Function: calculate_checksums * - * * - * Purpose: calculate check sums of configuration data * - * * - * Parameters: * - * * - * Return value: SUCCESS - calculated succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id) -{ - char *sql = NULL; - int sql_allocated = 16*1024, sql_offset = 0; - int t, f, res = SUCCEED; - - zabbix_log(LOG_LEVEL_DEBUG, "In calculate_checksums"); - - sql = zbx_malloc(sql, sql_allocated); - - for (t = 0; tables[t].table != 0; t++) { - /* Do not sync some of tables */ - if ((tables[t].flags & ZBX_SYNC) == 0) - continue; - - if (NULL != tablename && 0 != strcmp(tablename, tables[t].table)) - continue; - - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, -#ifdef HAVE_MYSQL - "%s select %d,'%s',%s,%d,concat(", -#else - "%s select %d,'%s',%s,%d,", -#endif - sql_offset > 0 ? "union all" : "insert into node_cksum (nodeid,tablename,recordid,cksumtype,cksum)", - nodeid, - tables[t].table, - tables[t].recid, - NODE_CKSUM_TYPE_NEW); - - for (f = 0; tables[t].fields[f].name != 0; f ++) { - if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - continue; - - if (strcmp(tables[t].recid, tables[t].fields[f].name) == 0) - continue; - - if (tables[t].fields[f].flags & ZBX_NOTNULL) { - switch ( tables[t].fields[f].type ) { - case ZBX_TYPE_ID : - case ZBX_TYPE_INT : - case ZBX_TYPE_UINT : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "%s", - tables[t].fields[f].name); - break; - default : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "md5(%s)", - tables[t].fields[f].name); - break; - } - } else { - switch ( tables[t].fields[f].type ) { - case ZBX_TYPE_ID : - case ZBX_TYPE_INT : - case ZBX_TYPE_UINT : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "case when %s is null then 'NULL' else %1$s end", - tables[t].fields[f].name); - break; - default : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "case when %s is null then 'NULL' else md5(%1$s) end", - tables[t].fields[f].name); - break; - } - } - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, -#ifdef HAVE_MYSQL - ",'%c',", -#else - "||'%c'||", -#endif - ZBX_CKSUM_DELIMITER); - } - - /* remove last delimiter */ - if (f > 0) { -#ifdef HAVE_MYSQL - sql_offset -= 5; - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, ")"); -#else - sql_offset -= 7; -#endif - } - - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, - " from %s where"ZBX_COND_NODEID, - tables[t].table, - ZBX_NODE(tables[t].recid,nodeid)); - - if (0 != id) { - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "and %s="ZBX_FS_UI64, - tables[t].recid, - id); - } - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "\n"); - } - if (SUCCEED == res && DBexecute("delete from node_cksum where nodeid=%d and cksumtype=%d", - nodeid, - NODE_CKSUM_TYPE_NEW) < ZBX_DB_OK) - res = FAIL; - if (SUCCEED == res && DBexecute("%s", sql) < ZBX_DB_OK) - res = FAIL; - return res; -} - -/****************************************************************************** - * * - * Function: send_config_data * - * * - * Purpose: send configuration changes to required node * - * * - * Parameters: * - * * - * Return value: SUCCESS - processed succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -char *get_config_data(int nodeid, int dest_nodetype) -{ - DB_RESULT result; - DB_RESULT result2; - DB_ROW row; - DB_ROW row2; - - char *data = NULL, *hex = NULL, *sql = NULL, c, sync[131], *s, *r[2], *d[2]; - int data_offset=0, sql_offset = 0; - int data_allocated=1024, hex_allocated=1024, sql_allocated=8*1024; - int t, f, j, rowlen; - - zabbix_log( LOG_LEVEL_DEBUG, "In get_config_data(node:%d,dest_nodetype:%s)", - nodeid, - dest_nodetype == ZBX_NODE_MASTER ? "MASTER" : "SLAVE"); - - data = zbx_malloc(data, data_allocated); - hex = zbx_malloc(hex, hex_allocated); - sql = zbx_malloc(sql, sql_allocated); - c = '1'; - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "Data%c%d%c%d\n", - ZBX_DM_DELIMITER, - CONFIG_NODEID, - ZBX_DM_DELIMITER, - nodeid); - - /* Find updated records */ - result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum curr, node_cksum prev " - "where curr.nodeid=%1$d and prev.nodeid=%1$d and " - "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " - "curr.cksumtype=%3$d and prev.cksumtype=%2$d " - /*" and curr.tablename='hosts' "*/ - "union all " - /* Find new records */ - "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,curr.sync " - "from node_cksum curr left join node_cksum prev " - "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " - "prev.recordid=curr.recordid and prev.cksumtype=%2$d " - "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null " - /*" and curr.tablename='hosts' "*/ - "union all " - /* Find deleted records */ - "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum prev left join node_cksum curr " - "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " - "curr.recordid=prev.recordid and curr.cksumtype=%3$d " - "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null" - /*" and prev.tablename='hosts' "*/, - nodeid, - NODE_CKSUM_TYPE_OLD, /* prev */ - NODE_CKSUM_TYPE_NEW); /* curr */ - - while (NULL != (row = DBfetch(result))) { - for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) - ; - - /* Found table */ - if (tables[t].table == 0) { - zabbix_log( LOG_LEVEL_WARNING, "Cannot find table [%s]", - row[0]); - continue; - } - - if (DBis_null(row[4]) == FAIL) - strcpy(sync, row[4]); - else - memset(sync, ' ', sizeof(sync)); - s = sync; - - /* Special (simpler) processing for operation DELETE */ - if (DBis_null(row[2]) == FAIL && DBis_null(row[3]) == SUCCEED && - ((dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c))) { - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d\n", - row[0], - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - NODE_CONFIGLOG_OP_DELETE); - continue; - } - - r[0] = row[2]; - r[1] = row[3]; - f = 0; - sql_offset = 0; - - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "select "); - do { - if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - if (strcmp(tables[t].recid, tables[t].fields[f].name) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ZBX_CKSUM_DELIMITER))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ZBX_CKSUM_DELIMITER))) - *d[1] = '\0'; - - if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "%s,length(%1$s),", - tables[t].fields[f].name); - } - s += 2; - - if (d[0] != NULL) { - *d[0] = ZBX_CKSUM_DELIMITER; - r[0] = d[0] + 1; - } - if (d[1] != NULL) { - *d[1] = ZBX_CKSUM_DELIMITER; - r[1] = d[1] + 1; - } - - if (d[0] == NULL && d[1] == NULL) - break; - f++; - } while (1); - - if (sql[sql_offset-1] != ',') - continue; - - sql_offset--; - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, " from %s where %s=%s", - row[0], - tables[t].recid, - row[1]); - - result2 = DBselect("%s", sql); - if (NULL == (row2=DBfetch(result2))) - goto out; - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d", - row[0], - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - NODE_CONFIGLOG_OP_UPDATE); - - r[0] = row[2]; - r[1] = row[3]; - s = sync + 2; - f = 0; - j = 0; - - do { - if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - if (strcmp(tables[t].recid, tables[t].fields[f].name) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ZBX_CKSUM_DELIMITER))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ZBX_CKSUM_DELIMITER))) - *d[1] = '\0'; - - if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%c%s%c%d%c", - ZBX_DM_DELIMITER, - tables[t].fields[f].name, - ZBX_DM_DELIMITER, - tables[t].fields[f].type, - ZBX_DM_DELIMITER); - - /* Fieldname, type, value */ - if (DBis_null(row2[j*2]) == SUCCEED) { - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "NULL"); - } else if(tables[t].fields[f].type == ZBX_TYPE_INT || - tables[t].fields[f].type == ZBX_TYPE_UINT || - tables[t].fields[f].type == ZBX_TYPE_ID || - tables[t].fields[f].type == ZBX_TYPE_FLOAT) { - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s", row2[j*2]); - } else { - rowlen = atoi(row2[j*2+1]); - zbx_binary2hex((u_char *)row2[j*2], rowlen, &hex, &hex_allocated); - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, strlen(hex)+128, "%s", hex); -/*zabbix_log(LOG_LEVEL_CRIT, "----- [field:%s][type:%d][row:%s][hex:%s]",tables[t].fields[f].name,tables[t].fields[f].type,row2[j*2],hex);*/ - } - j++; - } - s += 2; - - if (d[0] != NULL) { - *d[0] = ZBX_CKSUM_DELIMITER; - r[0] = d[0] + 1; - } - if (d[1] != NULL) { - *d[1] = ZBX_CKSUM_DELIMITER; - r[1] = d[1] + 1; - } - - if (d[0] == NULL && d[1] == NULL) - break; - f++; - } while (1); - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n"); -out: - DBfree_result(result2); - } - DBfree_result(result); - - zbx_free(hex); - zbx_free(sql); - - return data; -} - -/****************************************************************************** - * * - * Function: update_checksums * - * * - * Purpose: overwrite old checksums with new ones * - * * - * Parameters: * - * * - * Return value: SUCCESS - calculated succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields) -{ - char *r[2], *d[2], sync[129], *s; - char c, sql[2][256]; - DB_RESULT result; - DB_ROW row; - int t, f; - - zabbix_log(LOG_LEVEL_DEBUG, "In update_checksums"); - - c = synked == SUCCEED ? '1' : ' '; - - if (NULL != tablename) { - zbx_snprintf(sql[0], sizeof(sql[0]), " and curr.tablename='%s' and curr.recordid="ZBX_FS_UI64, - tablename, id); - zbx_snprintf(sql[1], sizeof(sql[1]), " and prev.tablename='%s' and prev.recordid="ZBX_FS_UI64, - tablename, id); - } else { - *sql[0] = '\0'; - *sql[1] = '\0'; - } - - /* Find updated records */ - result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum curr, node_cksum prev " - "where curr.nodeid=%1$d and prev.nodeid=%1$d and " - "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " - "curr.cksumtype=%3$d and prev.cksumtype=%2$d%4$s " - "union all " - /* Find new records */ - "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,NULL " - "from node_cksum curr left join node_cksum prev " - "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " - "prev.recordid=curr.recordid and prev.cksumtype=%2$d " - "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null%4$s " - "union all " - /* Find deleted records */ - "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum prev left join node_cksum curr " - "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " - "curr.recordid=prev.recordid and curr.cksumtype=%3$d " - "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null%5$s", - nodeid, - NODE_CKSUM_TYPE_OLD, /* prev */ - NODE_CKSUM_TYPE_NEW, /* curr */ - sql[0], - sql[1]); - - while (NULL != (row = DBfetch(result))) { - for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) - ; - - /* Found table */ - if (tables[t].table == 0) { - zabbix_log(LOG_LEVEL_WARNING, "Cannot find table [%s]", - row[0]); - continue; - } - - if (DBis_null(row[4]) == FAIL) - strcpy(sync, row[4]); - else - memset(sync, ' ', sizeof(sync)); - s = sync; - - /* Special (simpler) processing for operation DELETE */ - if (DBis_null(row[3]) == SUCCEED) { - if (synked == SUCCEED) { - if (synked_nodetype == ZBX_NODE_SLAVE) - *s = c; - else if (synked_nodetype == ZBX_NODE_MASTER) - *(s+1) = c; - } - s += 2; - } else { - r[0] = row[2]; - r[1] = row[3]; - f = 0; - - do { - if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - if (strcmp(tables[t].recid, tables[t].fields[f].name) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ZBX_CKSUM_DELIMITER))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ZBX_CKSUM_DELIMITER))) - *d[1] = '\0'; - - if (NULL == tablename || SUCCEED == str_in_list(fields, tables[t].fields[f].name, ',')) { - if (r[0] == NULL || r[1] == NULL || strcmp(r[0], r[1]) != 0) { - if (synked_nodetype == ZBX_NODE_SLAVE) - *s = c; - else if (synked_nodetype == ZBX_NODE_MASTER) - *(s+1) = c; - } else { - if (synked == SUCCEED) { - if (synked_nodetype == ZBX_NODE_SLAVE) - *s = c; - else if (synked_nodetype == ZBX_NODE_MASTER) - *(s+1) = c; - } - } - } - s += 2; - - if (d[0] != NULL) { - *d[0] = ZBX_CKSUM_DELIMITER; - r[0] = d[0] + 1; - } - if (d[1] != NULL) { - *d[1] = ZBX_CKSUM_DELIMITER; - r[1] = d[1] + 1; - } - - if (d[0] == NULL && d[1] == NULL) - break; - f++; - } while (1); - } - *s = '\0'; - - if (DBis_null(row[2]) == SUCCEED || DBis_null(row[3]) == SUCCEED || - strcmp(row[4], sync) != 0 || strcmp(row[2], row[3]) != 0) { - DBexecute("update node_cksum set cksumtype=%d,cksum=\'%s\',sync=\'%s\' " - "where nodeid=%d and tablename=\'%s\' and recordid=%s and cksumtype=%d", - NODE_CKSUM_TYPE_OLD, - DBis_null(row[3]) == SUCCEED ? row[2] : row[3], - sync, - nodeid, - row[0], - row[1], - DBis_null(row[2]) == SUCCEED ? NODE_CKSUM_TYPE_NEW : NODE_CKSUM_TYPE_OLD); - } - } - DBfree_result(result); - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: node_sync_lock * - * * - * Purpose: * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void node_sync_lock(int nodeid) -{ - zbx_mutex_lock(&node_sync_access); -} - -/****************************************************************************** - * * - * Function: node_sync_unlock * + * Function: is_master_node * * * * Purpose: * * * * Parameters: * * * - * Return value: * + * Return value: SUCCEED - nodeid is master node * + * FAIL - nodeid is slave node * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ -void node_sync_unlock(int nodeid) -{ - zbx_mutex_unlock(&node_sync_access); -} - -/****************************************************************************** - * * - * Function: process_nodes * - * * - * Purpose: calculates checks sum of config data * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: never returns * - * * - ******************************************************************************/ -void process_nodes() +int is_master_node(int current_nodeid, int nodeid) { - DB_RESULT result; - DB_ROW row; - int nodeid; - int master_nodeid; - char *data, *answer; - zbx_sock_t sock; - int res; -/* int now = time(NULL);*/ - int sender_nodeid; - - master_nodeid = CONFIG_MASTER_NODEID; - if (0 == master_nodeid) - return; - - result = DBselect("select nodeid from nodes"); - while (NULL != (row=DBfetch(result))) { - nodeid = atoi(row[0]); - - node_sync_lock(nodeid); - -/* DBbegin();*/ - - res = calculate_checksums(nodeid, NULL, 0); - if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_MASTER))) { - zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Sending configuration changes to master node %d for node %d datalen %d", - CONFIG_NODEID, - master_nodeid, - nodeid, - strlen(data)); - if (SUCCEED == (res = connect_to_node(master_nodeid, &sock))) { - if (SUCCEED == res) - res = send_data_to_node(master_nodeid, &sock, data); - if (SUCCEED == res) - res = recv_data_from_node(master_nodeid, &sock, &answer); - if (SUCCEED == res) { - if (0 == strncmp(answer, "Data", 4) || 0 == strcmp(answer, "OK")) - res = update_checksums(nodeid, ZBX_NODE_MASTER, SUCCEED, NULL, 0, NULL); - if (0 == strncmp(answer, "Data", 4)) { - if (SUCCEED == res) - res = node_sync(answer, &sender_nodeid, &nodeid); - send_data_to_node(master_nodeid, &sock, SUCCEED == res ? "OK" : "FAIL"); - } - } - disconnect_node(&sock); - } - zbx_free(data); - } - -/* DBcommit();*/ - - node_sync_unlock(nodeid); + DB_RESULT dbresult; + DB_ROW dbrow; + int res = FAIL; + + dbresult = DBselect("select masterid from nodes where nodeid=%d", + current_nodeid); + + if (NULL != (dbrow = DBfetch(dbresult))) { + current_nodeid = atoi(dbrow[0]); + if (current_nodeid == nodeid) + res = SUCCEED; + else if (0 != current_nodeid) + res = is_master_node(current_nodeid, nodeid); } - DBfree_result(result); + DBfree_result(dbresult); -/* zabbix_log(LOG_LEVEL_CRIT, "<-----> process_nodes [Selected records in %d seconds]", time(NULL)-now);*/ + return res; } /****************************************************************************** diff --git a/src/zabbix_server/nodewatcher/nodewatcher.h b/src/zabbix_server/nodewatcher/nodewatcher.h index 30960667..60285d0f 100644 --- a/src/zabbix_server/nodewatcher/nodewatcher.h +++ b/src/zabbix_server/nodewatcher/nodewatcher.h @@ -20,16 +20,7 @@ #ifndef ZABBIX_NODEWATCHER_H #define ZABBIX_NODEWATCHER_H -#include "mutexs.h" - -extern ZBX_MUTEX node_sync_access; - -int main_nodewatcher_loop(); - -int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id); -char *get_config_data(int nodeid, int dest_nodetype); -int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields); -void node_sync_lock(int nodeid); -void node_sync_unlock(int nodeid); +int main_nodewatcher_loop(); +int is_master_node(int current_nodeid, int nodeid); #endif diff --git a/src/zabbix_server/trapper/nodehistory.c b/src/zabbix_server/trapper/nodehistory.c index 7798c99b..66a22092 100644 --- a/src/zabbix_server/trapper/nodehistory.c +++ b/src/zabbix_server/trapper/nodehistory.c @@ -59,90 +59,117 @@ * Comments: * * * ******************************************************************************/ -static int process_record(int nodeid, char *record) +static int process_record(int sender_nodeid, int nodeid, char *record, char **tmp, int *tmp_allocated) { - char *tmp = NULL, value_esc[MAX_STRING_LEN], source[MAX_STRING_LEN], *r; - int tmp_allocated = 1024, table; + char value_esc[MAX_STRING_LEN], source[MAX_STRING_LEN], *r; zbx_uint64_t id, itemid, value_uint; - int clock, timestamp, severity; - double value; + int table, clock, timestamp, severity; + double value_dbl; int res = FAIL; zabbix_log( LOG_LEVEL_DEBUG, "In process_record [%s]", record); - tmp = zbx_malloc(tmp, tmp_allocated); - r = record; - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - table = atoi(tmp); + if (NULL == r) + goto error; + + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + table = atoi(*tmp); + + if (NULL == r) + goto error; + + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + ZBX_STR2UINT64(itemid, *tmp); + + if (NULL == r) + goto error; - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - ZBX_STR2UINT64(itemid, tmp); + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + clock = atoi(*tmp); - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - clock = atoi(tmp); + if (NULL == r) + goto error; - if(table == ZBX_TABLE_HISTORY) - { - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - value = atof(tmp); + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); - res = DBadd_history(itemid, value, clock); + switch (table) { + case ZBX_TABLE_HISTORY : + value_dbl = atof(*tmp); - DBexecute("update items set lastvalue='" ZBX_FS_DBL "', lastclock=%d where itemid=" ZBX_FS_UI64, - value, + res = DBadd_history(itemid, value_dbl, clock); + + DBexecute("update items set lastvalue='"ZBX_FS_DBL"',lastclock=%d " + "where itemid="ZBX_FS_UI64, + value_dbl, clock, itemid); - - } - else if(table == ZBX_TABLE_HISTORY_UINT) - { - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - ZBX_STR2UINT64(value_uint, tmp); + break; + case ZBX_TABLE_HISTORY_UINT : + ZBX_STR2UINT64(value_uint, *tmp); res = DBadd_history_uint(itemid, value_uint, clock); - DBexecute("update items set lastvalue='" ZBX_FS_UI64 "', lastclock=%d where itemid=" ZBX_FS_UI64, + DBexecute("update items set lastvalue='"ZBX_FS_UI64"',lastclock=%d " + "where itemid="ZBX_FS_UI64, value_uint, clock, itemid); - } - else if(table == ZBX_TABLE_HISTORY_STR) - { - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); + break; + case ZBX_TABLE_HISTORY_STR : + zbx_hex2binary(*tmp); - res = DBadd_history_str(itemid, tmp, clock); + res = DBadd_history_str(itemid, *tmp, clock); - DBescape_string(tmp, value_esc, MAX_STRING_LEN); + DBescape_string(*tmp, value_esc, MAX_STRING_LEN); - DBexecute("update items set lastvalue='%s', lastclock=%d where itemid=" ZBX_FS_UI64, + DBexecute("update items set lastvalue='%s',lastclock=%d " + "where itemid=" ZBX_FS_UI64, value_esc, clock, itemid); - } - else if(table == ZBX_TABLE_HISTORY_LOG) - { - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - ZBX_STR2UINT64(id, tmp); + break; + case ZBX_TABLE_HISTORY_LOG : + ZBX_STR2UINT64(id, *tmp); - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - timestamp = atoi(tmp); + if (NULL == r) + goto error; - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - strcpy(source, tmp); + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + timestamp = atoi(*tmp); - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - severity = atoi(tmp); + if (NULL == r) + goto error; - r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - zbx_hex2binary(tmp); + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + strcpy(source, *tmp); - res = DBadd_history_log(id, itemid, tmp, clock, timestamp, source, severity); + if (NULL == r) + goto error; + + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + severity = atoi(*tmp); + + if (NULL == r) + goto error; + + r = zbx_get_next_field(r, tmp, tmp_allocated, ZBX_DM_DELIMITER); + zbx_hex2binary(*tmp); + + res = DBadd_history_log(id, itemid, *tmp, clock, timestamp, source, severity); + break; } - zbx_free(tmp); return res; +error: + zabbix_log( LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", + CONFIG_NODEID, + sender_nodeid, + nodeid, + record); + + return FAIL; } /****************************************************************************** @@ -163,14 +190,16 @@ static int process_record(int nodeid, char *record) ******************************************************************************/ int node_history(char *data) { - char *start, *newline = NULL, *tmp = NULL; - int tmp_allocated = 128; + char *r, *newline = NULL, *tmp = NULL; + int tmp_allocated = 1024; int firstline=1; int nodeid=0; int sender_nodeid=0; int datalen; - datalen=strlen(data); + assert(data); + + datalen = strlen(data); zabbix_log( LOG_LEVEL_DEBUG, "In node_history(len:%d)", datalen); @@ -178,44 +207,32 @@ int node_history(char *data) tmp = zbx_malloc(tmp, tmp_allocated); - for(start = data; *start != '\0';) - { - if(NULL != (newline = strchr(start, '\n'))) - { + for (r = data; *r != '\0';) { + if (NULL != (newline = strchr(r, '\n'))) *newline = '\0'; - } - if(firstline == 1) - { - zabbix_log( LOG_LEVEL_DEBUG, "In node_history() process header [%s]", start); - start = zbx_get_next_field(start, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* History */ - start = zbx_get_next_field(start, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); + if (firstline == 1) { +/* zabbix_log( LOG_LEVEL_DEBUG, "In node_history() process header [%s]", r);*/ + r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* constant 'History' */ + r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* sender_nodeid */ sender_nodeid=atoi(tmp); - start = zbx_get_next_field(start, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); + r = zbx_get_next_field(r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* nodeid */ nodeid=atoi(tmp); - firstline=0; zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Received history from node %d for node %d datalen %d", - CONFIG_NODEID, - sender_nodeid, - nodeid, - datalen); - } - else - { - process_record(nodeid, start); - } - - if(newline != NULL) - { + CONFIG_NODEID, + sender_nodeid, + nodeid, + datalen); + firstline=0; + } else + process_record(sender_nodeid, nodeid, r, &tmp, &tmp_allocated); + + if (newline != NULL) { *newline = '\n'; - start = newline + 1; - newline = NULL; - } - else - { + r = newline + 1; + } else break; - } } zbx_free(tmp); diff --git a/src/zabbix_server/trapper/nodesync.c b/src/zabbix_server/trapper/nodesync.c index 416fcbf8..6f0ce292 100644 --- a/src/zabbix_server/trapper/nodesync.c +++ b/src/zabbix_server/trapper/nodesync.c @@ -43,8 +43,7 @@ #include "dbsync.h" #include "nodesync.h" -#include "../nodewatcher/nodewatcher.h" -#include "../nodewatcher/nodecomms.h" +#include "../nodewatcher/nodesender.h" /****************************************************************************** * * @@ -83,7 +82,6 @@ static int process_record(int nodeid, char *record, int sender_nodetype) #if defined(HAVE_POSTGRESQL) int len; #endif /* HAVE_POSTGRESQL */ - int table_acknowledges = 0; zabbix_log( LOG_LEVEL_DEBUG, "In process_record [%s]", record); diff --git a/src/zabbix_server/trapper/trapper.c b/src/zabbix_server/trapper/trapper.c index 2f03a009..58e5167c 100644 --- a/src/zabbix_server/trapper/trapper.c +++ b/src/zabbix_server/trapper/trapper.c @@ -30,7 +30,7 @@ #include "../expression.h" #include "../nodewatcher/nodecomms.h" -#include "../nodewatcher/nodewatcher.h" +#include "../nodewatcher/nodesender.h" #include "nodesync.h" #include "nodeevents.h" #include "nodehistory.h" @@ -99,10 +99,9 @@ static int process_trap(zbx_sock_t *sock,char *s, int max_len) /* zabbix_log( LOG_LEVEL_WARNING, "Node data received [len:%d]", strlen(s)); */ res = node_sync(s, &sender_nodeid, &nodeid); - if (nodeid == CONFIG_NODEID || FAIL == res) - send_data_to_node(sender_nodeid, sock, SUCCEED == res ? "OK" : "FAIL"); - - if (nodeid != CONFIG_NODEID && SUCCEED == res) { + if (FAIL == res) + send_data_to_node(sender_nodeid, sock, "FAIL"); + else { res = calculate_checksums(nodeid, NULL, 0); if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_SLAVE))) { if (SUCCEED == res) |