📄 manage.sql
字号:
('serviceState','Tells us whether a service on a server is up or down.','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('moduleState','Tells us whether a module in a device is working or not.','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('thresholdState','Tells us whether the load has passed a certain threshold.','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('linkState','Tells us whether a link is up or down.','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('boxRestart','Tells us that a network-unit has done a restart','n');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('info','Basic information','n');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('notification','Notification event, typically between NAV systems','n');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('deviceActive','Lifetime event for a device','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('deviceState','Registers the state of a device','y');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('deviceNotice','Registers a notice on a device','n');INSERT INTO eventtype (eventtypeid,eventtypedesc,stateful) VALUES ('maintenanceState','Tells us if something is set on maintenance','y');CREATE TABLE eventq ( eventqid SERIAL PRIMARY KEY, source VARCHAR(32) NOT NULL REFERENCES subsystem (name) ON UPDATE CASCADE ON DELETE CASCADE, target VARCHAR(32) NOT NULL REFERENCES subsystem (name) ON UPDATE CASCADE ON DELETE CASCADE, deviceid INT4 REFERENCES device ON UPDATE CASCADE ON DELETE CASCADE, netboxid INT4 REFERENCES netbox ON UPDATE CASCADE ON DELETE CASCADE, subid VARCHAR, time TIMESTAMP NOT NULL DEFAULT NOW (), eventtypeid VARCHAR(32) NOT NULL REFERENCES eventtype ON UPDATE CASCADE ON DELETE CASCADE, state CHAR(1) NOT NULL DEFAULT 'x' CHECK (state='x' OR state='s' OR state='e'), -- x = stateless, s = start, e = end value INT4 NOT NULL DEFAULT '100', severity INT4 NOT NULL DEFAULT '50');CREATE INDEX eventq_target_btree ON eventq USING btree (target);CREATE TABLE eventqvar ( eventqid INT4 REFERENCES eventq ON UPDATE CASCADE ON DELETE CASCADE, var VARCHAR NOT NULL, val TEXT NOT NULL, UNIQUE(eventqid, var) -- only one val per var per event);CREATE INDEX eventqvar_eventqid_btree ON eventqvar USING btree (eventqid);-- alert tablesCREATE TABLE alerttype ( alerttypeid SERIAL PRIMARY KEY, eventtypeid VARCHAR(32) NOT NULL REFERENCES eventtype ON UPDATE CASCADE ON DELETE CASCADE, alerttype VARCHAR, alerttypedesc VARCHAR);INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxDownWarning','Warning sent before declaring the box down.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxShadowWarning','Warning sent before declaring the box in shadow.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxDown','Box declared down.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxUp','Box declared up.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxShadow','Box declared down, but is in shadow.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxState','boxSunny','Box declared up from a previous shadow state.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('moduleState','moduleDownWarning','Warning sent before declaring the module down.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('moduleState','moduleDown','Module declared down.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('moduleState','moduleUp','Module declared up.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('serviceState','httpDown','http service not responding.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('serviceState','httpUp','http service responding.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('maintenanceState','onMaintenance','Box put on maintenance.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('maintenanceState','offMaintenance','Box taken off maintenance.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('thresholdState','exceededThreshold','Threshold exceeded.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('thresholdState','belowThreshold','Value below threshold.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('info','dnsMismatch','Mismatch between sysname and dnsname.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('info','serialChanged','Serial number for the device has changed.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxRestart','coldStart','Tells us that a network-unit has done a coldstart.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('boxRestart','warmStart','Tells us that a network-unit has done a warmstart.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceState','deviceInIPOperation','Device is in operation as a box.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceState','deviceInStack','Device is in operation as a module.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceState','deviceRMA','RMA event for device.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceNotice','deviceError','Error situation on device.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceNotice','deviceSwUpgrade','Software upgrade on device.');INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES ('deviceNotice','deviceHwUpgrade','Hardware upgrade on device.');CREATE TABLE alertq ( alertqid SERIAL PRIMARY KEY, source VARCHAR(32) NOT NULL REFERENCES subsystem (name) ON UPDATE CASCADE ON DELETE CASCADE, deviceid INT4 REFERENCES device ON UPDATE CASCADE ON DELETE CASCADE, netboxid INT4 REFERENCES netbox ON UPDATE CASCADE ON DELETE CASCADE, subid VARCHAR, time TIMESTAMP NOT NULL, eventtypeid VARCHAR(32) REFERENCES eventtype ON UPDATE CASCADE ON DELETE CASCADE, alerttypeid INT4 REFERENCES alerttype ON UPDATE CASCADE ON DELETE CASCADE, state CHAR(1) NOT NULL, value INT4 NOT NULL, severity INT4 NOT NULL);CREATE TABLE alertqmsg ( alertqid INT4 REFERENCES alertq ON UPDATE CASCADE ON DELETE CASCADE, msgtype VARCHAR NOT NULL, language VARCHAR NOT NULL, msg TEXT NOT NULL, UNIQUE(alertqid, msgtype, language));CREATE INDEX alertqmsg_alertqid_btree ON alertqmsg USING btree (alertqid);CREATE TABLE alertqvar ( alertqid INT4 REFERENCES alertq ON UPDATE CASCADE ON DELETE CASCADE, var VARCHAR NOT NULL, val TEXT NOT NULL, UNIQUE(alertqid, var) -- only one val per var per event);CREATE INDEX alertqvar_alertqid_btree ON alertqvar USING btree (alertqid);CREATE TABLE alerthist ( alerthistid SERIAL PRIMARY KEY, source VARCHAR(32) NOT NULL REFERENCES subsystem (name) ON UPDATE CASCADE ON DELETE CASCADE, deviceid INT4 REFERENCES device ON UPDATE CASCADE ON DELETE CASCADE, netboxid INT4 REFERENCES netbox ON UPDATE CASCADE ON DELETE SET NULL, subid VARCHAR, start_time TIMESTAMP NOT NULL, end_time TIMESTAMP DEFAULT 'infinity', eventtypeid VARCHAR(32) NOT NULL REFERENCES eventtype ON UPDATE CASCADE ON DELETE CASCADE, alerttypeid INT4 REFERENCES alerttype ON UPDATE CASCADE ON DELETE CASCADE, value INT4 NOT NULL, severity INT4 NOT NULL);CREATE INDEX alerthist_end_time_btree ON alerthist USING btree (end_time);CREATE TABLE alerthistmsg ( alerthistid INT4 REFERENCES alerthist ON UPDATE CASCADE ON DELETE CASCADE, state CHAR(1) NOT NULL, msgtype VARCHAR NOT NULL, language VARCHAR NOT NULL, msg TEXT NOT NULL, UNIQUE(alerthistid, state, msgtype, language));CREATE INDEX alerthistmsg_alerthistid_btree ON alerthistmsg USING btree (alerthistid);CREATE TABLE alerthistvar ( alerthistid INT4 REFERENCES alerthist ON UPDATE CASCADE ON DELETE CASCADE, state CHAR(1) NOT NULL, var VARCHAR NOT NULL, val TEXT NOT NULL, UNIQUE(alerthistid, state, var) -- only one val per var per state per alert);CREATE INDEX alerthistvar_alerthistid_btree ON alerthistvar USING btree (alerthistid);-------------------------------------------------------------------------------- servicemon tables------------------------------------------------------------------------------CREATE TABLE service ( serviceid SERIAL PRIMARY KEY, netboxid INT4 REFERENCES netbox ON UPDATE CASCADE ON DELETE CASCADE, active BOOL DEFAULT true, handler VARCHAR, version VARCHAR, up CHAR(1) NOT NULL DEFAULT 'y' CHECK (up='y' OR up='n' OR up='s') -- y=up, n=down, s=shadow);CREATE RULE rrdfile_deleter AS ON DELETE TO service DO DELETE FROM rrd_file WHERE key='serviceid' AND value=old.serviceid;CREATE TABLE serviceproperty (serviceid INT4 NOT NULL REFERENCES service ON UPDATE CASCADE ON DELETE CASCADE, property VARCHAR(64) NOT NULL, value VARCHAR, PRIMARY KEY(serviceid, property));-------------------------------------------------------------------------------- messages/maintenance v2 tables------------------------------------------------------------------------------CREATE TABLE message ( messageid SERIAL PRIMARY KEY, title VARCHAR NOT NULL, description TEXT NOT NULL, tech_description TEXT, publish_start TIMESTAMP, publish_end TIMESTAMP, author VARCHAR NOT NULL, last_changed TIMESTAMP, replaces_message INT REFERENCES message, replaced_by INT REFERENCES message);CREATE OR REPLACE FUNCTION message_replace() RETURNS TRIGGER AS ' DECLARE -- Old replaced_by value of the message beeing replaced old_replaced_by INTEGER; BEGIN -- Remove references that are no longer correct IF TG_OP = ''UPDATE'' THEN IF OLD.replaces_message <> NEW.replaces_message OR (OLD.replaces_message IS NOT NULL AND NEW.replaces_message IS NULL) THEN EXECUTE ''UPDATE message SET replaced_by = NULL WHERE messageid = '' || quote_literal(OLD.replaces_message); END IF; END IF; -- It does not replace any message, exit IF NEW.replaces_message IS NULL THEN RETURN NEW; END IF; -- Update the replaced_by field of the replaced message with a -- reference to the replacer SELECT INTO old_replaced_by replaced_by FROM message WHERE messageid = NEW.replaces_message; IF old_replaced_by <> NEW.messageid OR old_replaced_by IS NULL THEN EXECUTE ''UPDATE message SET replaced_by = '' || quote_literal(NEW.messageid) || '' WHERE messageid = '' || quote_literal(NEW.replaces_message); END IF; RETURN NEW; END; ' language 'plpgsql';CREATE TRIGGER trig_message_replace AFTER INSERT OR UPDATE ON message FOR EACH ROW EXECUTE PROCEDURE message_replace();CREATE OR REPLACE VIEW message_with_replaced AS SELECT m.messageid, m.title, m.description, m.tech_description, m.publish_start, m.publish_end, m.author, m.last_changed, m.replaces_message, m.replaced_by, rm.title AS replaces_message_title, rm.description AS replaces_message_description, rm.tech_description AS replaces_message_tech_description, rm.publish_start AS replaces_message_publish_start, rm.publish_end AS replaces_message_publish_end, rm.author AS replaces_message_author, rm.last_changed AS replaces_message_last_changed, rb.title AS replaced_by_title, rb.description AS replaced_by_description, rb.tech_description AS replaced_by_tech_description, rb.publish_start AS replaced_by_publish_start, rb.publish_end AS replaced_by_publish_end, rb.author AS replaced_by_author, rb.last_changed AS replaced_by_last_changed FROM message m LEFT JOIN message rm ON (m.replaces_message = rm.messageid) LEFT JOIN message rb ON (m.replaced_by = rb.messageid);CREATE TABLE maint_task ( maint_taskid SERIAL PRIMARY KEY, maint_start TIMESTAMP NOT NULL, maint_end TIMESTAMP NOT NULL, description TEXT NOT NULL, author VARCHAR NOT NULL, state VARCHAR NOT NULL);CREATE TABLE maint_component ( maint_taskid INT NOT NULL REFERENCES maint_task ON UPDATE CASCADE ON DELETE CASCADE, key VARCHAR NOT NULL, value VARCHAR NOT NULL, PRIMARY KEY (maint_taskid, key, value));CREATE TABLE message_to_maint_task ( messageid INT NOT NULL REFERENCES message ON UPDATE CASCADE ON DELETE CASCADE, maint_taskid INT NOT NULL REFERENCES maint_task ON UPDATE CASCADE ON DELETE CASCADE, PRIMARY KEY (messageid, maint_taskid));CREATE OR REPLACE VIEW maint AS SELECT * FROM maint_task NATURAL JOIN maint_component;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -