
create table host (
    host_id integer primary key not null,
    host_name varchar(50) not null,
    host_ip varchar(16) not null,
    enabled char(1) default 'Y' not null
);

create sequence seq_host_id increment 1 start 1;

create table service (
    service_id integer primary key not null,
    service_name varchar(20) not null,
    service_code varchar(10) not null,
    service_class varchar(100)
);

create sequence seq_service_id increment 1 start 1;

insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'Ping', 'PING', 'org.gumbynet.carnivore.helpers.PINGLinHelper');
insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'SMTP', 'SMTP', 'org.gumbynet.carnivore.helpers.SMTPHelper');
insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'HTTP', 'HTTP', 'org.gumbynet.carnivore.helpers.HTTPHelper');
insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'POP3', 'POP3', 'org.gumbynet.carnivore.helpers.POP3Helper');
insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'FTP', 'FTP', 'org.gumbynet.carnivore.helpers.FTPHelper');
insert into service (service_id, service_name, service_code, service_class) values (nextval('seq_service_id'), 'SSH', 'SSH', 'org.gumbynet.carnivore.helpers.SSHHelper');


create table host_services (
    host_id integer references host(host_id) on delete cascade not null ,
    service_id integer references service(service_id) on delete cascade not null,
    enabled char(1) default 'Y' not null,
    last_probed integer default 0
);

insert into host (host_id, host_name, host_ip, enabled) values (nextval('seq_host_id'), 'vorlon.rendrag.net', '202.128.97.20', 'Y');
insert into host_services (host_id, service_id) values (1, 1);
insert into host_services (host_id, service_id) values (1, 2);
insert into host_services (host_id, service_id) values (1, 3);
insert into host_services (host_id, service_id) values (1, 4);
insert into host_services (host_id, service_id) values (1, 5);
insert into host_services (host_id, service_id) values (1, 6);

insert into host (host_id, host_name, host_ip, enabled) values (nextval('seq_host_id'), 'box3n.gumbynet.org', '202.128.111.7', 'Y');
insert into host_services (host_id, service_id) values (2, 1);
insert into host_services (host_id, service_id) values (2, 2);
insert into host_services (host_id, service_id) values (2, 3);
insert into host_services (host_id, service_id) values (2, 5);
insert into host_services (host_id, service_id) values (2, 6);

insert into host (host_id, host_name, host_ip, enabled) values (nextval('seq_host_id'), 'echelon.pinegap.net', '202.128.111.9', 'Y');
insert into host_services (host_id, service_id) values (3, 1);
insert into host_services (host_id, service_id) values (3, 2);
insert into host_services (host_id, service_id) values (3, 3);
insert into host_services (host_id, service_id) values (3, 5);
insert into host_services (host_id, service_id) values (3, 6);

insert into host (host_id, host_name, host_ip, enabled) values (nextval('seq_host_id'), 'geblah.networkcentral.org', '202.128.97.8', 'Y');
insert into host_services (host_id, service_id) values (4, 1);
insert into host_services (host_id, service_id) values (4, 2);
insert into host_services (host_id, service_id) values (4, 3);
insert into host_services (host_id, service_id) values (4, 4);
insert into host_services (host_id, service_id) values (4, 5);
insert into host_services (host_id, service_id) values (4, 6);

insert into host (host_id, host_name, host_ip, enabled) values (nextval('seq_host_id'), 'terra.rendrag.net', '202.128.96.137', 'Y');
insert into host_services (host_id, service_id) values (5, 1);
insert into host_services (host_id, service_id) values (5, 3);
insert into host_services (host_id, service_id) values (5, 5);

create table host_log (
    host_id integer references host(host_id) not null,
    service_id integer references service(service_id) not null,
    log_datetime bigint not null,
    log_status integer not null,
    log_title varchar(300) NOT NULL,
    log_text varchar(2000) NOT NULL
);
create index host_log_datetime on host_log(log_datetime);
create index host_log_host_service on host_log(host_id, service_id);


create table host_statuschange (
    host_id integer references host(host_id) not null,
    service_id integer references service(service_id) not null,
    log_datetime bigint not null,
    log_oldstatus integer not null,
    log_newstatus integer not null
);
create index host_statuschange_datetime on host_statuschange(log_datetime);
create index host_statuschange_host_service on host_statuschange(host_id, service_id);


create table auditlog (
    auditlog_logtime  INTEGER NOT NULL,
    auditlog_module varchar(40) NOT NULL,
    auditlog_severity varchar(10) NOT NULL,
    auditlog_message varchar(500) NOT NULL
);

