Setup the groups and database permissions. The three groups
[gnucomo.git] / src / database / create.sql
1 --*************************************************************************
2 --  (c) Copyright 2002, De Winter Information Solutions
3 -- This is free software; you can redistribute it and/or modify it under the
4 -- terms of the GNU General Public License, see the file COPYING.
5 --*************************************************************************/
6 --
7 -- Gnucomo database creation script.
8 --
9 --
10 -- This SQL script creates the initial tables for the Gnucomo database.
11 -- Before running this script with 'psql -f', you should have your
12 -- DBA create the database and give access permissions.
13 --
14 --  $Log: create.sql,v $
15 --  Revision 1.10  2003-02-14 06:32:27  arjen
16 --  Setup the groups and database permissions. The three groups
17 --  are: view, ops and admin.
18 --  Added a new entry to the action table.
19 --
20 --  Revision 1.9  2003/02/13 08:46:54  arjen
21 --  Added log, notification and parameter counters to the 'object' table.
22 --  Counting these things at the time a user interface needs them is
23 --  too slow. Other programs, like gcm_daemon en gcm_input should prepare
24 --  these counters for quick retrieval.
25 --
26 --  Revision 1.8  2003/02/08 07:36:41  arjen
27 --  Added new table to the database : log_adv_daemon
28 --
29 --  Revision 1.7  2003/02/05 09:29:08  arjen
30 --  Bug fix: action.statuscode was in upper case.
31 --
32 --  Revision 1.6  2003/01/20 07:29:48  arjen
33 --  Added new tables to the database: parameter_class and parameter_notification
34 --
35 --  Revision 1.5  2003/01/18 08:46:48  arjen
36 --  Added new records to the 'type_of_issue' table.
37 --  Changed semantics of actionid 9 in the 'action' table.
38 --
39 --
40
41 CREATE SEQUENCE "action_actionid_seq";
42
43 CREATE TABLE "action"
44 (
45         "actionid" bigint DEFAULT nextval('"action_actionid_seq"'::text) NOT NULL,
46         "actionname" text,
47         "statuscode" character varying(3),
48         "description" text
49 );
50
51
52 COPY "action" FROM stdin;
53 1       Entry in the system     new     This indicates that a notification has been entered into the system.
54 2       Display to user opn     The notification has been displayed to the user. It doesn't mean that the user actually read the notification, but he/she should be aware. For that reason we consider the notification to be OPEN
55 3       Remarks added   pen     Remarks have been added to the notification.
56 4       Priority changed manually       pen     The priority of the notification has been changed by the user.
57 5       Priority changed automatically  pen     The priority of the notification has been changed by the system. This can be the result of an action by the user or an automatical escalation.
58 6       Action taken    pen     An action has been taken.
59 7       Assignment to user      pen     The notification has been assigned to an user.
60 8       More information or research needed.    inv     The notification is relevant and will be handled, however more information or research will be needed. For that reason the status has been altered to UNDER INVESTIGATION.
61 9       Investigation completed pen     Investigation has been done. Information is available to fix the problem.
62 10      Job output no longer valid      cls     By making a newer job output a valid reference, this data has been obsoleted. Since it was a reference once the output isn't interesting anymore and thus the notification can be closed.
63 11      Action taken - please verify    cls     An action has been taken and things should have been resolved. Before the notification can be closed a verification has to be done. The new status is now VERIFY.
64 12      Action not verified     pen     A check has been done and the results were not good. New verification is needed. The status has been changed back to PEN.
65 13      Action verified cls     A check has been done and the results were approved. The notification has been closed.
66 15      SMS sent        opn     An SMS has been sent.
67 14      E-mail sent     opn     An e-mail has been sent.
68 16      Fax sent        opn     An fax has been sent.
69 17      Log entries shown       \N      The log entries relevant to the notification have been shown.
70 18      Notification closed     cls     The notification has been closed.
71 19      Notification reopend    opn     The notification has been reopend.
72 20      Redisplayed to user     \N      The notification has been presented to a user. This is not the first time
73 \.
74
75 CREATE UNIQUE INDEX action_actionid_key ON "action" USING btree (actionid);
76
77 CREATE UNIQUE INDEX act_pk ON "action" USING btree (actionid);
78
79 CREATE UNIQUE INDEX act_actionname ON "action" USING btree (actionname);
80
81 CREATE INDEX act_statuscode ON "action" USING btree (statuscode);
82
83 SELECT setval ('"action_actionid_seq"', 19, true);
84
85 --
86 --
87
88 CREATE SEQUENCE "action_user_actionstepid_seq";
89
90 CREATE TABLE "action_user"
91 (
92         "actionstepid" bigint DEFAULT
93             nextval('"action_user_actionstepid_seq"'::text) NOT NULL,
94         "actionid" bigint,
95         "username" text,
96         "notificationid" bigint,
97         "timestamp" timestamp with time zone,
98         "statuscode" character varying(3),
99         "remarks" text
100 );
101
102 CREATE UNIQUE INDEX action_user_actionstepid_key ON action_user USING btree (actionstepid);
103
104 CREATE INDEX anu_actionid ON action_user USING btree (actionid);
105
106 CREATE INDEX anu_username ON action_user USING btree (username);
107
108 CREATE INDEX anu_notificationid ON action_user USING btree (notificationid);
109
110 CREATE INDEX anu_timestamp ON action_user USING btree ("timestamp");
111
112 CREATE INDEX anu_statuscode ON action_user USING btree (statuscode);
113
114 SELECT setval ('"action_user_actionstepid_seq"', 1, false);
115
116 --
117 --
118
119 CREATE TABLE "db_value"
120 (
121         "setting" text,
122         "setting_value" text
123 );
124
125
126 COPY "db_value" FROM stdin;
127 db_version      28
128 gcm_daemon_version      1
129 log_processing  0
130 last_notification       0
131 \.
132
133 --
134 --
135
136 CREATE TABLE "history"
137 (
138    paramid            bigint,
139    modified           timestamp,
140    change_nature      text,       --  CREATED, MODIFIED or REMOVED
141    changed_property   text,
142    new_value          text,
143    remark             text
144
145 );
146
147 --
148 --
149
150 CREATE SEQUENCE "log_logid_seq";
151
152 CREATE TABLE "log"
153 (
154         "logid" bigint DEFAULT nextval('"log_logid_seq"'::text) NOT NULL,
155         "objectid" bigint,
156         "original_filename" text,
157         "servicecode" text,
158         "object_timestamp" timestamp with time zone,
159         "timestamp" timestamp with time zone,
160         "rawdata" text,
161         "processed" boolean DEFAULT false,
162         "recognized" boolean DEFAULT false
163 );
164
165 CREATE UNIQUE INDEX log_logid_key ON log USING btree (logid);
166
167 CREATE INDEX log_objectid ON log USING btree (objectid);
168
169 CREATE INDEX log_original_filename ON log USING btree (original_filename);
170
171 CREATE INDEX log_servicecode ON log USING btree (servicecode);
172
173 CREATE INDEX log_object_timestmap ON log USING btree ("timestamp");
174
175 CREATE INDEX log_timestmap ON log USING btree ("timestamp");
176
177 CREATE INDEX log_timestamp ON log USING btree (object_timestamp);
178
179 CREATE INDEX log_processed ON log USING btree (processed);
180
181 SELECT setval ('"log_logid_seq"', 1, false);
182
183
184 --
185 --
186
187 CREATE SEQUENCE "log_advid_seq";
188
189
190 CREATE TABLE "log_adv"
191 (
192         "log_advid" bigint DEFAULT
193               nextval('"log_advid_seq"'::text) NOT NULL,
194         "logid" bigint NOT NULL,
195         "detailed_table"    text
196 );
197
198
199 CREATE INDEX log_adv_logid ON log_adv USING btree (logid);
200
201 CREATE UNIQUE INDEX log_adv_log_advid ON log_adv USING btree (log_advid);
202
203 SELECT setval ('"log_advid_seq"', 1, false);
204
205 CREATE TABLE log_adv_kernel_network
206 (
207        device_in         text,
208        device_out        text,
209        hw_address        text, 
210        source_ip         INET,
211        destination_ip    INET,
212        packet_length     bigint, 
213        tos_bit           text, 
214        prec_bit          text,
215        ttl               int,
216        header_id         bigint,
217        source_port       int, 
218        destination_port  int,
219        body_length       int,
220        protocol          text, 
221        body_len          int,
222        window            text,
223        urgp              int,
224        syn               boolean DEFAULT false,
225        type              int,
226        code              int,
227        sequence_number   int,
228        res               text,
229        rst               boolean,
230        df                boolean
231 ) INHERITS (log_adv);
232
233 CREATE TABLE log_adv_daemon
234 (
235        service TEXT,
236        event TEXT
237 ) INHERITS (log_adv);
238
239 CREATE INDEX log_adv_daemon_service ON log_adv_daemon (service);
240
241 /*
242 CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER "plpgsql_call_handler" LANCOMPILER '';
243 CREATE FUNCTION "funct_processlog" () RETURNS opaque AS '
244 -- Initial date: September 18th 2002
245 -- Update: November 13th 2002
246 -- Author: Brenno J.S.A.A.F. de Winter
247 -- Abstract: This routine sets a flag
248 DECLARE
249    var_value    VARCHAR;
250    var_setting  VARCHAR;
251
252 BEGIN
253
254     UPDATE db_value SET setting_value = ''TRUE'' WHERE setting = ''log_flag'';
255     RETURN NULL;
256 END;
257  ' LANGUAGE 'plpgsql';
258
259
260 CREATE TRIGGER "log_insert" AFTER INSERT ON "log"  FOR EACH ROW EXECUTE PROCEDURE "funct_processlog" ();
261 */
262 --
263 --
264
265 CREATE TABLE "log_notification"
266 (
267         "notificationid" bigint,
268         "logid" bigint
269 );
270
271 CREATE UNIQUE INDEX lon_pk ON log_notification USING btree (notificationid, logid);
272
273 CREATE UNIQUE INDEX lon_notificationid ON log_notification USING btree (notificationid);
274
275 CREATE UNIQUE INDEX lon_logid ON log_notification USING btree (logid);
276
277 --
278 --
279
280 CREATE SEQUENCE "notification_notificationid_seq";
281
282 CREATE TABLE "notification"
283 (
284         "notificationid" bigint DEFAULT
285             nextval('"notification_notificationid_seq"'::text) NOT NULL,
286         "objectid" bigint,
287         "type_of_issueid" bigint,
288         "timestamp" timestamp with time zone,
289         "statuscode" character varying(3),
290         "priority" integer,
291         "escalation_count_timestamp" timestamp with time zone,
292         "repeat_notification_timestamp" timestamp with time zone,
293         "securitylevel_view" integer,
294         "securitylevel_add" integer,
295         "securitylevel_close" integer
296 );
297
298 CREATE UNIQUE INDEX notification_notificationid_key ON notification USING btree (notificationid);
299
300 CREATE INDEX not_objectid ON notification USING btree (objectid);
301
302 CREATE INDEX not_type_of_issueid ON notification USING btree (type_of_issueid);
303
304 CREATE INDEX not_timestamp ON notification USING btree ("timestamp");
305
306 CREATE INDEX not_statuscode ON notification USING btree (statuscode);
307
308 CREATE INDEX not_priority ON notification USING btree (priority);
309
310 CREATE INDEX not_escalation_count_timestamp ON notification USING btree (escalation_count_timestamp);
311
312 CREATE INDEX not_repeat_notification_timesta ON notification USING btree (repeat_notification_timestamp);
313
314 SELECT setval ('"notification_notificationid_seq"', 1, false);
315
316 --
317 --
318
319 CREATE SEQUENCE "object_objectid_seq";
320
321 CREATE TABLE "object"
322 (
323         "objectid" bigint DEFAULT nextval('"object_objectid_seq"'::text) NOT NULL,
324         "objectname" text,
325         "objectcode" text,
326         "scp_enabled" boolean,
327         "scp_inet" inet,
328         "mail_enabled" boolean,
329         "mail_from" text,
330         "sms_enabled" boolean,
331         "sms_number" text,
332         "fax_enabled" boolean,
333         "fax_number" text,
334         "object_description" text,
335         "object_owner" text,
336         "physical_location" text,
337         "timezone" text,
338         "remark" text,
339         "os"     text,
340         "os_version"   text,
341         "log_count"    bigint,
342         "notification_count"   bigint,
343         "parameter_count"      bigint
344 );
345
346 CREATE UNIQUE INDEX object_objectid_key ON object USING btree (objectid);
347
348 CREATE UNIQUE INDEX obj_objectname ON object USING btree (objectname);
349
350 CREATE UNIQUE INDEX obj_objectcode ON object USING btree (objectcode);
351
352 CREATE INDEX obj_mail_from ON object USING btree (mail_from);
353
354 CREATE INDEX os ON object (os);
355
356 CREATE INDEX os_version ON object (os, os_version);
357
358 SELECT setval ('"object_objectid_seq"', 1, false);
359
360 --
361 --
362
363 CREATE TABLE "object_issue"
364 (
365         "objectid" bigint,
366         "type_of_issueid" bigint,
367         "default_priority" integer,
368         "escalation" boolean,
369         "escalation_time" time without time zone,
370         "max_priority" integer,
371         "adjust_setting" text
372 );
373
374 CREATE UNIQUE INDEX obj_pk ON object_issue USING btree (objectid, type_of_issueid);
375
376 CREATE INDEX obj_objectid ON object_issue USING btree (objectid);
377
378 CREATE UNIQUE INDEX obj_type_of_notificationid ON object_issue USING btree (type_of_issueid);
379
380 --
381 --
382
383 CREATE TABLE "object_priority"
384 (
385         "objectid" bigint,
386         "priorityid" integer,
387         "send_mail" boolean,
388         "send_sms" boolean,
389         "send_fax" boolean,
390         "repeat_notification" boolean,
391         "interval_for_repeat" time without time zone
392 );
393
394 CREATE UNIQUE INDEX obi_pk ON object_priority USING btree (objectid, priorityid);
395
396 CREATE INDEX obi_objectid ON object_priority USING btree (objectid);
397
398 CREATE INDEX obi_priorityid ON object_priority USING btree (priorityid);
399
400 --
401 --
402
403 CREATE TABLE "object_service"
404 (
405         "objectid" bigint,
406         "servicecode" text,
407         "expected_interval" bigint,
408         "last_entry" timestamp with time zone,
409         "default_priority" integer,
410         "maximum_priority" integer,
411         "accepted" boolean
412 );
413
414 CREATE UNIQUE INDEX obs_pk ON object_service USING btree (objectid, servicecode);
415
416 CREATE INDEX obs_objectid ON object_service USING btree (objectid);
417
418 CREATE INDEX obs_servicecode ON object_service USING btree (servicecode);
419
420 CREATE INDEX obs_accepted ON object_service USING btree (accepted);
421
422 --
423 --
424
425 CREATE TABLE "object_user"
426 (
427         "objectid" bigint,
428         "username" text,
429         "security_level" integer
430 );
431
432 CREATE UNIQUE INDEX ous_pk ON object_user USING btree (objectid, username);
433
434 CREATE INDEX ous_objectid ON object_user USING btree (objectid);
435
436 CREATE INDEX ous_username ON object_user USING btree (username);
437
438 CREATE INDEX ous_security_level ON object_user USING btree (security_level);
439
440 --
441 --
442
443 CREATE SEQUENCE "paramid_seq";
444
445 CREATE TABLE "parameter"
446 (
447         "paramid" bigint DEFAULT nextval('"paramid_seq"'::text) NOT NULL,
448         "objectid" bigint,
449         "name" text,
450         "class" text,
451         "description" text,
452
453         primary key (paramid)
454 );
455
456 CREATE UNIQUE INDEX param_obj_name ON parameter USING btree (objectid, name, class);
457
458 SELECT setval ('"paramid_seq"', 1, true);
459
460 --
461 --
462
463 CREATE TABLE "parameter_class"
464 (
465    "name"             text,     --  Name of the class: see parameter.class
466    "property_name"    text,
467    "description"      text,
468    "property_type"    text,     --  STATIC or DYNAMIC
469    "min"              float,    --  Default minimum value
470    "max"              float,    --  Default maximum value
471    "notify"           boolean,  --  Notify if something changes ?
472
473    primary key (name, property_name)
474 );
475
476 INSERT INTO parameter_class (name, property_name, description, property_type, notify)
477   VALUES ('package', 'version', 'The installed version of the package', 'STATIC', 't');
478
479 --
480 --
481
482 CREATE TABLE "parameter_notification"
483 (
484    "notificationid" bigint,
485    "paramid"        bigint,
486
487    primary key (notificationid, paramid)
488 );
489
490 --
491 --
492
493 CREATE TABLE "priority"
494 (
495         "priority" integer,
496         "send_mail" boolean,
497         "send_sms" boolean,
498         "send_fax" boolean,
499         "repeat_notification" boolean,
500         "interval_for_repeat" time without time zone
501 );
502
503
504 CREATE UNIQUE INDEX pri_pk ON priority USING btree (priority);
505
506 --
507 --
508
509 CREATE TABLE "property"
510 (
511    paramid bigint,
512    name    text,
513    value   text,
514    type    text,   --   STATIC or DYNAMIC
515    min     float,
516    max     float,
517
518    primary key (paramid, name)
519 );
520
521 --
522 --
523
524 CREATE TABLE "service"
525 (
526         "servicecode" text,
527         "servicename" text,
528         "default_priority" integer,
529         "max_priority" integer
530 );
531
532 COPY "service" FROM stdin;
533 httpd   httpd   1       5
534 imap    imap    1       5
535 imapd   imapd   1       5
536 kernel  kernel  1       5
537 sshd    sshd    1       5
538 su      su      1       5
539 syslogd syslogd 1       5
540 \.
541
542 CREATE UNIQUE INDEX ser_pk ON service USING btree (servicecode);
543
544 CREATE UNIQUE INDEX ser_servicename ON service USING btree (servicename);
545
546 --
547
548 CREATE TABLE "status"
549 (
550         "statuscode" character varying(3),
551         "statusname" text,
552         "open_notification" boolean,
553         "description" text
554 );
555
556
557 COPY "status" FROM stdin;
558 new     new entry       t       Just detected, but nothing has been done yet
559 opn     open notification       t       The notification has been displayed to a user or a user has been notified. However nothing has been done yet.
560 pen     pending t       The notification is currently being worked on.
561 ver     waiting for verification        t       The notification has been worked on and is currently awaiting the approval/verification.
562 rej     rejected        f       The notification has been identified as a false postive and was reject. The notification is now closed
563 cls     closed  f       The notification has been closed
564 inv     needs investigation     t       The notification is currently under investigation and is awaiting additional details before one can work on this again.
565 \.
566
567 CREATE UNIQUE INDEX sta_pk ON status USING btree (statuscode);
568
569 CREATE UNIQUE INDEX sta_statusname ON status USING btree (statusname);
570
571 CREATE INDEX sta_open_notification ON status USING btree (open_notification);
572
573 --
574 --
575
576 CREATE TABLE supported_os
577 (
578     os_name text,
579     remarks text
580 );
581
582 CREATE UNIQUE INDEX spp_os ON supported_os (os_name);
583
584 --
585 --
586
587 CREATE SEQUENCE "type_of_issue_type_of_issue_seq";
588
589 CREATE TABLE "type_of_issue"
590 (
591         "type_of_issueid" bigint DEFAULT
592            nextval('"type_of_issue_type_of_issue_seq"'::text) NOT NULL,
593         "name" text,
594         "suggested_priority" text,
595         "description" text,
596         "active" boolean,
597         automated_check       boolean,
598         alert_level           int,
599         last_run              timestamp,
600         recheck_interval      timestamp
601 );
602
603
604 COPY "type_of_issue" FROM stdin;
605 1       manual entry    4       A manual entry of a notification        t
606 2       parameter created       3       A new parameter was created     t
607 3       property modified       3       The STATIC property of a parameter was modified t
608 4       parameter removed       3       A parameter was removed t
609 \.
610
611 CREATE UNIQUE INDEX type_of_issue_type_of_issue_key ON type_of_issue USING btree (type_of_issueid);
612
613 CREATE UNIQUE INDEX toi_name ON type_of_issue USING btree (name);
614
615 CREATE INDEX toi_active ON type_of_issue USING btree (active);
616
617 SELECT setval ('"type_of_issue_type_of_issue_seq"', 4, true);
618
619 --
620 --
621
622 CREATE TABLE "usr"
623 (
624         "username" text NOT NULL,
625         "active_sessionid" bigint,
626         "account_active" boolean,
627         "security_level" integer
628 );
629
630 CREATE UNIQUE INDEX usr_username ON usr USING btree (username);
631
632 CREATE UNIQUE INDEX usr_active_sessionid ON usr USING btree (active_sessionid);
633
634 CREATE INDEX usr_account_active ON usr USING btree (account_active);
635
636 CREATE INDEX usr_security_level ON usr USING btree (security_level);
637
638 --
639 --  Set up user groups and grant permissions in the proper places.
640
641 CREATE GROUP view;
642 CREATE GROUP ops;
643 CREATE GROUP admin;
644
645 GRANT SELECT ON action TO GROUP view, GROUP ops, GROUP admin;
646 GRANT SELECT ON action_user TO GROUP view, GROUP ops, GROUP admin;
647 GRANT SELECT ON db_value TO GROUP view, GROUP ops, GROUP admin;
648 GRANT SELECT ON history TO GROUP view, GROUP ops, GROUP admin;
649 GRANT SELECT ON log TO GROUP view, GROUP ops, GROUP admin;
650 GRANT SELECT ON log_adv TO GROUP view, GROUP ops, GROUP admin;
651 GRANT SELECT ON log_adv_daemon TO GROUP view, GROUP ops, GROUP admin;
652 GRANT SELECT ON log_adv_kernel_network TO GROUP view, GROUP ops, GROUP admin;
653 GRANT SELECT ON log_notification TO GROUP view, GROUP ops, GROUP admin;
654 GRANT SELECT ON notification TO GROUP view, GROUP ops, GROUP admin;
655 GRANT SELECT ON object TO GROUP view, GROUP ops, GROUP admin;
656 GRANT SELECT ON object_issue TO GROUP view, GROUP ops, GROUP admin;
657 GRANT SELECT ON object_priority TO GROUP view, GROUP ops, GROUP admin;
658 GRANT SELECT ON object_service TO GROUP view, GROUP ops, GROUP admin;
659 GRANT SELECT ON object_user TO GROUP view, GROUP ops, GROUP admin;
660 GRANT SELECT ON parameter TO GROUP view, GROUP ops, GROUP admin;
661 GRANT SELECT ON parameter_class TO GROUP view, GROUP ops, GROUP admin;
662 GRANT SELECT ON parameter_notification TO GROUP view, GROUP ops, GROUP admin;
663 GRANT SELECT ON priority TO GROUP view, GROUP ops, GROUP admin;
664 GRANT SELECT ON property TO GROUP view, GROUP ops, GROUP admin;
665 GRANT SELECT ON service TO GROUP view, GROUP ops, GROUP admin;
666 GRANT SELECT ON status TO GROUP view, GROUP ops, GROUP admin;
667 GRANT SELECT ON supported_os TO GROUP view, GROUP ops, GROUP admin;
668 GRANT SELECT ON type_of_issue TO GROUP view, GROUP ops, GROUP admin;
669 GRANT SELECT ON usr TO GROUP view, GROUP ops, GROUP admin;
670
671 GRANT INSERT ON action_user TO GROUP ops, GROUP admin;
672 GRANT UPDATE ON notification TO GROUP ops, GROUP admin;
673 GRANT INSERT ON object GROUP admin;
674 GRANT UPDATE ON object TO GROUP ops, GROUP admin;
675 GRANT DELETE ON object GROUP admin;
676 GRANT UPDATE ON usr TO GROUP view, GROUP ops, GROUP admin;
677 GRANT INSERT ON usr TO GROUP admin;
678 GRANT DELETE ON usr TO GROUP admin;