33 |
rodolico |
1 |
/*
|
|
|
2 |
The base install of CAMP2.
|
|
|
3 |
|
|
|
4 |
Just track devices. A device can be any arbitrary item.
|
|
|
5 |
A device has one owner, and is located at one site (location)
|
|
|
6 |
A site also has an owner. Devices can be located on locations
|
|
|
7 |
owned by a different client, ie when a device is lent to a client
|
|
|
8 |
or colocated at a third party NOC.
|
|
|
9 |
|
|
|
10 |
This is very basic. It only tracks device ownership and locations,
|
|
|
11 |
and movement between them (via the created and removed fields).
|
|
|
12 |
When a device is moved to a different location, or sold to a different
|
|
|
13 |
client, the old record in the linking field has its removed field
|
|
|
14 |
updated and a new record created with a created date. With this, we
|
|
|
15 |
can track the lifespan of a device.
|
|
|
16 |
|
|
|
17 |
It is assumed various modules will be created to extend the capabilities
|
|
|
18 |
of this basic structure. However, modules should not modify the basic
|
|
|
19 |
database structure, instead creating new tables that link into
|
|
|
20 |
these tables.
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/*
|
|
|
24 |
configuration of application. DB representation of old Windows INI file format
|
|
|
25 |
containing groups, and under groups key/value pairs
|
|
|
26 |
*/
|
|
|
27 |
drop table if exists _system;
|
|
|
28 |
create table _system (
|
|
|
29 |
_system_id int unsigned not null auto_increment,
|
|
|
30 |
group_name varchar(45) not null comment 'Group name for matching',
|
|
|
31 |
key_name varchar(45) not null comment 'key name for matching',
|
|
|
32 |
key_value varchar(45) not null comment 'value for key_name',
|
40 |
rodolico |
33 |
unique key unique_group_name( group_name,key_name ),
|
33 |
rodolico |
34 |
primary key (_system_id)
|
|
|
35 |
)
|
|
|
36 |
comment = 'Stores internal system information like ini file';
|
|
|
37 |
|
|
|
38 |
/*
|
|
|
39 |
holds menu, which may be modified programmatically.
|
|
|
40 |
This is a hierarchial menu so an entry may have a parent entry
|
|
|
41 |
*/
|
|
|
42 |
drop table if exists _menu;
|
|
|
43 |
create table _menu (
|
|
|
44 |
_menu_id int unsigned not null auto_increment,
|
37 |
rodolico |
45 |
parent_id int unsigned default null references _menu (_menu_id),
|
33 |
rodolico |
46 |
caption varchar(20) not null comment 'Caption displayed for menu',
|
|
|
47 |
url varchar(64) default null comment 'optional URL when they click here',
|
|
|
48 |
primary key (_menu_id)
|
|
|
49 |
) comment = 'Menus for the application';
|
|
|
50 |
|
37 |
rodolico |
51 |
insert into camp2._menu values (null,null,'Home', '/index.php');
|
33 |
rodolico |
52 |
|
|
|
53 |
/*
|
|
|
54 |
simple table to hold ownership information
|
|
|
55 |
*/
|
|
|
56 |
drop table if exists client;
|
|
|
57 |
create table client (
|
|
|
58 |
client_id int unsigned not null auto_increment,
|
|
|
59 |
name varchar(64) comment 'name of client',
|
|
|
60 |
created date comment 'date record was created',
|
|
|
61 |
removed date comment 'date record was removed',
|
|
|
62 |
primary key (client_id)
|
|
|
63 |
) comment 'hold client information';
|
|
|
64 |
|
|
|
65 |
/*
|
|
|
66 |
simple table to hold site where a device is located
|
|
|
67 |
*/
|
|
|
68 |
drop table if exists site;
|
|
|
69 |
create table site (
|
|
|
70 |
site_id int unsigned not null auto_increment,
|
|
|
71 |
name varchar(64) comment 'name of site',
|
|
|
72 |
created date comment 'date record was created',
|
|
|
73 |
removed date comment 'date record was removed',
|
|
|
74 |
primary key (site_id)
|
|
|
75 |
) comment 'hold site information';
|
|
|
76 |
|
|
|
77 |
/*
|
|
|
78 |
table which holds a device type, such as server, workstation
|
|
|
79 |
printer, virtual, etc...
|
|
|
80 |
*/
|
|
|
81 |
drop table if exists device_type;
|
|
|
82 |
create table device_type (
|
|
|
83 |
device_type_id int unsigned not null auto_increment,
|
|
|
84 |
name varchar(64) comment 'name of device type',
|
|
|
85 |
show_as_system boolean comment 'if true, this is a system, ie a computer or virtual',
|
|
|
86 |
created date comment 'date record was created',
|
|
|
87 |
removed date comment 'date record was removed',
|
|
|
88 |
primary key (device_type_id)
|
|
|
89 |
) comment 'simple child table to determine the type of device we have';
|
|
|
90 |
|
|
|
91 |
/*
|
40 |
rodolico |
92 |
holds very basic information on a device such as its name and a unique id.
|
|
|
93 |
This is the main table for the database, and each device
|
33 |
rodolico |
94 |
should be uniquely identified. We will allow name to be modified
|
|
|
95 |
randomly, however.
|
|
|
96 |
|
|
|
97 |
Internally, we find this device based on device_id, but for remote
|
|
|
98 |
systems, we use the combined uuid and serial to uniquely identify
|
|
|
99 |
some manufacturers use one uuid for all systems, but the uuid and serial
|
|
|
100 |
number combination should be unique. serial can be any arbitrary string
|
|
|
101 |
and it is suggested to use manufacturer:serial or something like that
|
|
|
102 |
*/
|
|
|
103 |
drop table if exists device;
|
|
|
104 |
create table device (
|
|
|
105 |
device_id int unsigned not null auto_increment,
|
40 |
rodolico |
106 |
uuid varchar(36) comment 'unique id of this device, normally uuid',
|
33 |
rodolico |
107 |
serial varchar(32) comment 'serial number of this device, if we have it',
|
|
|
108 |
name varchar(64) comment 'name of device',
|
|
|
109 |
created date comment 'date record was created',
|
|
|
110 |
removed date comment 'date record was removed',
|
40 |
rodolico |
111 |
unique key unique_uuid( uuid, serial ),
|
33 |
rodolico |
112 |
primary key (device_id)
|
|
|
113 |
) comment 'holds individual devices';
|
|
|
114 |
|
40 |
rodolico |
115 |
/*
|
|
|
116 |
Many to many join table allowing devices to have multiple device
|
|
|
117 |
types. NOTE: I'm using device_id and device_type_id as the composite
|
|
|
118 |
primary key, so no duplicates, and we don't need an 'id' column
|
|
|
119 |
*/
|
33 |
rodolico |
120 |
|
40 |
rodolico |
121 |
drop table if exists device_device_type;
|
|
|
122 |
create table device_device_type (
|
|
|
123 |
device_id int unsigned not null references device( device_id ),
|
|
|
124 |
device_type_id int unsigned not null references device_type( device_type_id ),
|
|
|
125 |
primary key (device_id,device_type_id)
|
|
|
126 |
) comment 'many to many join for device and device_type tables';
|
|
|
127 |
|
33 |
rodolico |
128 |
/*
|
|
|
129 |
Set ownership of a site. These records are not deleted, but by
|
|
|
130 |
setting field removed to non-null value, then creating a new record,
|
|
|
131 |
we can track ownership of sites.
|
|
|
132 |
*/
|
|
|
133 |
drop table if exists client_site;
|
|
|
134 |
create table client_site (
|
|
|
135 |
client_site_id int unsigned not null auto_increment,
|
|
|
136 |
client_id int unsigned not null references client( client_id ),
|
|
|
137 |
site_id int unsigned not null references site( site_id ),
|
|
|
138 |
created date comment 'date record was created',
|
|
|
139 |
removed date comment 'date record was removed',
|
|
|
140 |
index site_device ( client_id,site_id ),
|
|
|
141 |
primary key (client_site_id)
|
|
|
142 |
) comment 'links ownership of a site to a client';
|
|
|
143 |
|
|
|
144 |
/*
|
|
|
145 |
Set location of a device. These records are not deleted, but by
|
|
|
146 |
setting field removed to non-null value, then creating a new record,
|
|
|
147 |
we can track movement of devices.
|
|
|
148 |
*/
|
|
|
149 |
drop table if exists site_device;
|
|
|
150 |
create table site_device (
|
|
|
151 |
site_device_id int unsigned not null auto_increment,
|
|
|
152 |
site_id int unsigned not null references site( site_id ),
|
|
|
153 |
device_id int unsigned not null references device( device_id ),
|
|
|
154 |
created date comment 'date record was created',
|
|
|
155 |
removed date comment 'date record was removed',
|
|
|
156 |
index site_device ( site_id,device_id ),
|
|
|
157 |
primary key (site_device_id)
|
|
|
158 |
) comment 'links a device to its location';
|
|
|
159 |
|
|
|
160 |
/*
|
|
|
161 |
Set ownership of a device. These records are not deleted, but by
|
|
|
162 |
setting field removed to non-null value, then creating a new record,
|
|
|
163 |
we can track ownership of devices.
|
|
|
164 |
*/
|
|
|
165 |
drop table if exists client_device;
|
|
|
166 |
create table client_device (
|
|
|
167 |
client_device_id int unsigned not null auto_increment,
|
|
|
168 |
client_id int unsigned not null references client( client_id ),
|
|
|
169 |
device_id int unsigned not null references device( device_id ),
|
|
|
170 |
created date comment 'date record was created',
|
|
|
171 |
removed date comment 'date record was removed',
|
|
|
172 |
index client_device( client_id, device_id ),
|
|
|
173 |
primary key ( client_device_id )
|
|
|
174 |
) comment 'links a device to its owner';
|
|
|
175 |
|
|
|
176 |
/*
|
|
|
177 |
There can be a parent/child relationship with devices. For example, a virtual
|
|
|
178 |
resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
|
|
|
179 |
or, a printer can be attached to a computer, so the printers parent is the
|
|
|
180 |
device_id of the computer it is attached to.
|
|
|
181 |
*/
|
|
|
182 |
drop table if exists device_device;
|
|
|
183 |
create table device_device (
|
|
|
184 |
device_device_id int unsigned not null auto_increment,
|
|
|
185 |
device_id int unsigned not null references device( device_id ),
|
|
|
186 |
parent_id int unsigned not null references device( device_id ),
|
|
|
187 |
created date comment 'date record was created',
|
|
|
188 |
removed date comment 'date record was removed',
|
|
|
189 |
index device_device( device_id, parent_id ),
|
|
|
190 |
primary key ( device_device_id )
|
|
|
191 |
) comment 'links a device to another device';
|
|
|
192 |
|
|
|
193 |
|
40 |
rodolico |
194 |
/*
|
|
|
195 |
Some views so we don't have to reinvent the wheel when we're trying
|
|
|
196 |
to grab some data
|
|
|
197 |
*/
|
|
|
198 |
|
|
|
199 |
/*
|
|
|
200 |
a simple view that concats the values in device_device_type for
|
|
|
201 |
display. Since mySQL will not allow subqueries in views, required
|
|
|
202 |
to have this information in view_device_site_client_type
|
|
|
203 |
*/
|
|
|
204 |
create view view_device_types as
|
|
|
205 |
select
|
|
|
206 |
device_id,
|
|
|
207 |
group_concat(distinct device_type.name) as device_types
|
|
|
208 |
from
|
|
|
209 |
device_device_type
|
|
|
210 |
join device_type using (device_type_id )
|
|
|
211 |
group by device_id
|
|
|
212 |
order by name;
|
|
|
213 |
|
|
|
214 |
/*
|
|
|
215 |
Mongo view that gets all the information together to display
|
|
|
216 |
device name, location, owner and type(s)
|
|
|
217 |
*/
|
|
|
218 |
|
|
|
219 |
drop view if exists view_device_site_client_type;
|
|
|
220 |
create view view_device_site_client_type as
|
33 |
rodolico |
221 |
select
|
40 |
rodolico |
222 |
device.device_id device_id,
|
|
|
223 |
device.uuid uuid,
|
|
|
224 |
device.serial serial,
|
33 |
rodolico |
225 |
device.name device,
|
|
|
226 |
device.created device_created,
|
|
|
227 |
device.removed device_removed,
|
40 |
rodolico |
228 |
view_device_types.device_types,
|
|
|
229 |
site.site_id site_id,
|
33 |
rodolico |
230 |
site.name site,
|
|
|
231 |
site.created site_created,
|
|
|
232 |
site.removed site_removed,
|
40 |
rodolico |
233 |
client.client_id client_id,
|
33 |
rodolico |
234 |
client.name client,
|
|
|
235 |
client.created client_created,
|
|
|
236 |
client.removed client_removed
|
|
|
237 |
from
|
|
|
238 |
device
|
40 |
rodolico |
239 |
join view_device_types using (device_id )
|
33 |
rodolico |
240 |
join site_device using (device_id)
|
|
|
241 |
join site using (site_id)
|
|
|
242 |
join client_device using (device_id)
|
40 |
rodolico |
243 |
join client using (client_id);
|
36 |
rodolico |
244 |
|
40 |
rodolico |
245 |
|
|
|
246 |
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' )
|
|
|
247 |
on duplicate key update key_value = '0.1';
|