1
BEGIN
2
DECLARE tp VARCHAR(20000);
3
DECLARE lu DATETIME;
4
IF LEFT(StatusString,4) <> '' THEN
5
SELECT CONCAT('Update computers SET LastContact=NOW()',IF(StatusString=',','',StatusString),' WHERE tempfiles<>-1 and ComputerID=',CID) INTO @tp;
6
PREPARE stmt2 FROM @tp;
7
EXECUTE stmt2;
8
DEALLOCATE PREPARE stmt2;
9
SET @tp='';
10
ELSE
11
UPDATE computers SET LastContact=NOW() WHERE tempfiles<>-1 and ComputerID=CID;
12
END IF;
13
IF FailedMons <> '' THEN
14
SELECT CONCAT('UPDATE h_agents JOIN agents USING(agentid) SET agents.LastScan=NOW(), h_agents.LastComputerID=',CID,',h_agents.FailureRate=ROUND((h_agents.Fails/(h_agents.Fails+h_agents.Checks))*100), h_agents.Fails=h_agents.Fails+1 WHERE h_agents.AgentID IN (',FailedMons,');') INTO @tp;
15
PREPARE stmt2 FROM @tp;
16
EXECUTE stmt2;
17
DEALLOCATE PREPARE stmt2;
18
SET @tp='';
19
END IF;
20
IF SuccessMons <> '' THEN
21
SELECT CONCAT('UPDATE h_agents JOIN agents USING(agentid) SET agents.LastScan=NOW(), h_agents.LastComputerID=',CID,',h_agents.FailureRate=ROUND((h_agents.Fails/(h_agents.Fails+h_agents.Checks))*100), h_agents.Checks=h_agents.Checks+1 WHERE h_agents.AgentID IN (',SuccessMons,');') INTO @tp;
22
PREPARE stmt2 FROM @tp;
23
EXECUTE stmt2;
24
DEALLOCATE PREPARE stmt2;
25
SET @tp='';
26
END IF;
27
IF CheckInCPU >0 OR CheckInMEM >0 OR BtIN>0 OR BtOUT > 0 THEN
28
INSERT INTO h_ComputerStats (ComputerID,CPU,MEM,DataIn,DataOut,EventDate,Samples) VALUES(CID,CheckInCPU,CheckInMEM,BtOUT,BtIN,DATE(NOW()),1) ON DUPLICATE KEY UPDATE `CPU`=ROUND(((`Samples` * `CPU`)+(VALUES(`CPU`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),`MEM`=ROUND(((`Samples` * `MEM`)+(VALUES(`MEM`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),`DataIn`=ROUND(((`Samples` * `DataIn`)+(VALUES(`DataIn`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),`DataOut`=ROUND(((`Samples` * `DataOut`)+(VALUES(`DataOut`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))), Samples=Samples+1;
29
SELECT lastupdate INTO lu FROM h_computerstatshourly WHERE computerid = CID ORDER BY eventdate DESC, eventhour DESC LIMIT 1;
30
IF TIMESTAMPDIFF(MINUTE,lu,NOW()) >= 5 OR ISNULL(lu) THEN
31
INSERT INTO h_computerstatshourly (ComputerID,CPU,MaxCPU,MinCPU,MEM,MaxMEM,MinMEM,DataIn,DataOut,EventDate,EventHour,Samples) VALUES(CID,CheckInCPU,CheckInCPU,CheckInCPU,CheckInMEM,CheckInMEM,CheckInMEM,BtOUT,BtIN,DATE(NOW()),HOUR(NOW()),1) ON DUPLICATE KEY UPDATE
32
`CPU`=ROUND(((`Samples` * `CPU`)+(VALUES(`CPU`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),
33
`MaxCPU`=IF(VALUES(CPU)>`MaxCPU`,VALUES(`CPU`),`MaxCPU`),
34
`MinCPU`=IF(VALUES(CPU)<`MinCPU`,VALUES(`CPU`),`MinCPU`),
35
`MEM`=ROUND(((`Samples` * `MEM`)+(VALUES(`MEM`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),
36
`MaxMEM`=IF(VALUES(MEM)>`MaxMEM`,VALUES(`MEM`),`MaxMEM`),
37
`MinMEM`=IF(VALUES(MEM)<`MinMEM`,VALUES(`MEM`),`MinMEM`),
38
`DataIn`=ROUND(((`Samples` * `DataIn`)+(VALUES(`DataIn`) * VALUES(`Samples`)))/(`Samples` + VALUES(`Samples`))),
39
Samples=Samples+1;
40
END IF;
41
END IF;
42
END