Subversion Repositories computer_asset_manager_v2

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 rodolico 1
/*
69 rodolico 2
Remove everything with
46 rodolico 3
drop table if exists _menu;
4
drop table if exists _system;
5
drop table if exists device;
6
drop table if exists device_device;
7
drop table if exists device_device_type;
8
drop table if exists device_type;
74 rodolico 9
drop table if exists location;
10
drop table if exists location_device;
11
drop table if exists mapping;
12
drop table if exists owner;
13
drop table if exists owner_device;
14
drop table if exists owner_location;
15
drop view if exists view_device;
16
drop view if exists view_device_device;
17
drop view if exists view_device_location_owner_type;
46 rodolico 18
drop view if exists view_device_types;
74 rodolico 19
drop view if exists view_location_device;
20
drop view if exists view_owner_device;
21
drop view if exists view_owner_location;
46 rodolico 22
 
23
*/
24
 
25
/*
33 rodolico 26
   The base install of CAMP2.
27
 
28
   Just track devices. A device can be any arbitrary item.
46 rodolico 29
   A device has one owner, and is located at one location (location)
30
   A location also has an owner. Devices can be located on locations
31
   owned by a different owner, ie when a device is lent to a owner
33 rodolico 32
   or colocated at a third party NOC.
33
 
34
   This is very basic. It only tracks device ownership and locations,
35
   and movement between them (via the created and removed fields).
36
   When a device is moved to a different location, or sold to a different
46 rodolico 37
   owner, the old record in the linking field has its removed field 
33 rodolico 38
   updated and a new record created with a created date. With this, we
39
   can track the lifespan of a device.
40
 
41
   It is assumed various modules will be created to extend the capabilities
42
   of this basic structure. However, modules should not modify the basic
43
   database structure, instead creating new tables that link into
44
   these tables.
45
*/
46
 
47
/*
48
   configuration of application. DB representation of old Windows INI file format
49
   containing groups, and under groups key/value pairs
50
*/
51
drop table if exists _system;
52
create table _system (
53
  _system_id      int unsigned not null auto_increment,
54
  group_name      varchar(45) not null comment 'Group name for matching',
55
  key_name        varchar(45) not null comment 'key name for matching',
55 rodolico 56
  key_value       text not null comment 'value for key_name',
40 rodolico 57
  unique key      unique_group_name( group_name,key_name ),
33 rodolico 58
  primary key     (_system_id)
59
)
60
comment = 'Stores internal system information like ini file';
61
 
62
/*
63
   holds menu, which may be modified programmatically.
64
   This is a hierarchial menu so an entry may have a parent entry
65
*/
66
drop table if exists _menu;
67
create table _menu (
68
   _menu_id       int unsigned not null auto_increment,
37 rodolico 69
   parent_id      int unsigned default null references _menu (_menu_id),
33 rodolico 70
   caption        varchar(20) not null comment 'Caption displayed for menu',
71
   url            varchar(64) default null comment 'optional URL when they click here',
46 rodolico 72
   shortname      varchar(16) default null comment 'used for permissions to determine if we display',
33 rodolico 73
   primary key    (_menu_id)
74
) comment = 'Menus for the application';
75
 
46 rodolico 76
/*
77
   insert the menu options for the main program
78
*/
74 rodolico 79
insert into _menu values (null,null,'Home', '/index.php', 'menu_home' );
80
insert into _menu select null,_menu_id,'Owners', '/index.php?module=Owner', 'menu_owner' from _menu where caption = 'Home';
81
insert into _menu select null,_menu_id,'Locations', '/index.php?module=Location', 'menu_location' from _menu where caption = 'Home';
82
insert into _menu select null,_menu_id,'Devices', '/index.php?module=Device', 'menu_device' from _menu where caption = 'Home';
83
insert into _menu values (null,null,'Reports', '/index.php?module=report', 'menu_report' );
33 rodolico 84
 
73 rodolico 85
/* 
86
   Modules are allowed to do the same thing for different classes, ie
87
   attribute can have a name for an Owner, and a name for a Location
88
   This table allows us to indicate which base table an entry is for
89
*/
90
 
91
drop table if exists _base_class;
92
create table _base_class (
93
   _base_class_id int unsigned not null auto_increment,
94
   name     varchar(64) comment 'name for display',
95
   class_name  varchar(64) comment 'actual class in program',
96
   base_table varchar(64) comment 'name of table which holds the class data',
97
   primary key (_base_class_id)
98
) comment 'a list of base classes';
99
 
100
insert into _base_class values (null,'Owner','Owner','owner');
101
insert into _base_class values (null,'Location','Location','location');
102
insert into _base_class values (null,'Device','Device','device');
103
 
104
 
33 rodolico 105
/*
106
   simple table to hold ownership information
107
*/
46 rodolico 108
drop table if exists owner;
109
create table owner (
110
   owner_id       int unsigned not null auto_increment,
111
   name           varchar(64) comment 'name of owner',
63 rodolico 112
   uuid           varchar(36) comment 'unique id of this owner, normally uuid',
64 rodolico 113
   created        date default current_timestamp comment 'date record was created',
33 rodolico 114
   removed        date comment 'date record was removed',
46 rodolico 115
   primary key    (owner_id)
116
) comment 'hold owner information';
33 rodolico 117
 
118
/*
46 rodolico 119
   simple table to hold location where a device is located
33 rodolico 120
*/
46 rodolico 121
drop table if exists location;
122
create table location (
123
   location_id    int unsigned not null auto_increment,
124
   name           varchar(64) comment 'name of location',
63 rodolico 125
   uuid           varchar(36) comment 'unique id of this location, normally uuid',
64 rodolico 126
   created        date default current_timestamp comment 'date record was created',
33 rodolico 127
   removed        date comment 'date record was removed',
46 rodolico 128
   primary key    (location_id)
129
) comment 'hold location information';
33 rodolico 130
 
131
/*
132
   table which holds a device type, such as server, workstation
133
   printer, virtual, etc...
134
*/
135
drop table if exists device_type;
136
create table device_type (
137
   device_type_id int unsigned not null auto_increment,
138
   name           varchar(64) comment 'name of device type',
64 rodolico 139
   created        date default current_timestamp comment 'date record was created',
33 rodolico 140
   removed        date comment 'date record was removed',
141
   primary key    (device_type_id)
142
) comment 'simple child table to determine the type of device we have';
143
 
144
/*
40 rodolico 145
   holds very basic information on a device such as its name and a unique id.
146
   This is the main table for the database, and each device
33 rodolico 147
   should be uniquely identified. We will allow name to be modified
148
   randomly, however.
149
 
150
   Internally, we find this device based on device_id, but for remote
151
   systems, we use the combined uuid and serial to uniquely identify
152
   some manufacturers use one uuid for all systems, but the uuid and serial
153
   number combination should be unique. serial can be any arbitrary string
154
   and it is suggested to use manufacturer:serial or something like that
155
*/
156
drop table if exists device;
157
create table device (
158
   device_id      int unsigned not null auto_increment,
40 rodolico 159
   uuid           varchar(36) comment 'unique id of this device, normally uuid',
33 rodolico 160
   serial         varchar(32) comment 'serial number of this device, if we have it',
161
   name           varchar(64) comment 'name of device',
64 rodolico 162
   created        date default current_timestamp comment 'date record was created',
33 rodolico 163
   removed        date comment 'date record was removed',
40 rodolico 164
   unique key     unique_uuid( uuid, serial ),
33 rodolico 165
   primary key    (device_id)
166
) comment 'holds individual devices';
167
 
40 rodolico 168
/*
169
   Many to many join table allowing devices to have multiple device
170
   types. NOTE: I'm using device_id and device_type_id as the composite
171
   primary key, so no duplicates, and we don't need an 'id' column
172
*/
33 rodolico 173
 
40 rodolico 174
drop table if exists device_device_type;
175
create table device_device_type (
176
   device_id      int unsigned not null references device( device_id ),
177
   device_type_id int unsigned not null references device_type( device_type_id ),
178
   primary key (device_id,device_type_id)
179
) comment 'many to many join for device and device_type tables';
180
 
33 rodolico 181
/*
46 rodolico 182
   Set ownership of a location. These records are not deleted, but by
33 rodolico 183
   setting field removed to non-null value, then creating a new record,
46 rodolico 184
   we can track ownership of locations.
33 rodolico 185
*/
46 rodolico 186
drop table if exists owner_location;
187
create table owner_location (
188
   owner_id      int unsigned not null references owner( owner_id ),
189
   location_id        int unsigned not null references location( location_id ),
64 rodolico 190
   created        date default current_timestamp comment 'date record was created',
33 rodolico 191
   removed        date comment 'date record was removed',
66 rodolico 192
   unique key    (owner_id,location_id, removed)
46 rodolico 193
) comment 'links ownership of a location to a owner';   
33 rodolico 194
 
195
/*
196
   Set location of a device. These records are not deleted, but by
197
   setting field removed to non-null value, then creating a new record,
198
   we can track movement of devices.
199
*/
46 rodolico 200
drop table if exists location_device;
201
create table location_device (
202
   location_id        int unsigned not null references location( location_id ),
33 rodolico 203
   device_id      int unsigned not null references device( device_id ),
64 rodolico 204
   created        date default current_timestamp comment 'date record was created',
33 rodolico 205
   removed        date comment 'date record was removed',
66 rodolico 206
   unique key    (location_id,device_id, removed)
33 rodolico 207
) comment 'links a device to its location';
208
 
209
/*
210
   Set ownership of a device. These records are not deleted, but by
211
   setting field removed to non-null value, then creating a new record,
212
   we can track ownership of devices.
213
*/
46 rodolico 214
drop table if exists owner_device;
215
create table owner_device (
216
   owner_id      int unsigned not null references owner( owner_id ),
33 rodolico 217
   device_id      int unsigned not null references device( device_id ),
64 rodolico 218
   created        date default current_timestamp comment 'date record was created',
66 rodolico 219
   removed        date default null comment 'date record was removed',
220
   unique key    ( owner_id, device_id, removed )
33 rodolico 221
) comment 'links a device to its owner';
222
 
223
/*
224
   There can be a parent/child relationship with devices. For example, a virtual
225
   resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
226
   or, a printer can be attached to a computer, so the printers parent is the
227
   device_id of the computer it is attached to.
228
*/
229
drop table if exists device_device;
230
create table device_device (
231
   device_id      int unsigned not null references device( device_id ),
232
   parent_id      int unsigned not null references device( device_id ),
64 rodolico 233
   created        date default current_timestamp comment 'date record was created',
66 rodolico 234
   removed        date default null comment 'date record was removed',
235
   unique key    ( device_id, parent_id, removed )
33 rodolico 236
) comment 'links a device to another device';
237
 
238
 
40 rodolico 239
/* 
240
   Some views so we don't have to reinvent the wheel when we're trying
241
   to grab some data
242
*/
243
 
244
/*
245
  a simple view that concats the values in device_device_type for 
246
  display. Since mySQL will not allow subqueries in views, required
46 rodolico 247
  to have this information in view_device_location_owner_type
40 rodolico 248
*/
46 rodolico 249
create or replace view view_device_types as
40 rodolico 250
   select 
251
      device_id,
63 rodolico 252
      group_concat(distinct device_type.name) as device_types,
253
      group_concat( distinct device_type.device_type_id) as device_type_ids
40 rodolico 254
   from 
255
      device_device_type 
256
      join device_type using (device_type_id ) 
257
   group by device_id 
258
   order by name;
59 rodolico 259
 
260
/*
261
   View to show relationships between machines, ie the part_of scheme
262
   where one device is actually a "part of" another. Used mainly with
263
   virtualization to show a virtual machine is on a particular 
264
   hypervisor
265
*/
266
create or replace view view_device_device as
267
   select 
268
      device_device.device_id,
269
      device.name device_name,
270
      device_device.parent_id,
271
      parent.name parent_name,
272
      device_device.created,
273
      device_device.removed 
274
   from 
275
      device 
276
      join device_device using (device_id) 
66 rodolico 277
      join device parent on (device_device.parent_id = parent.device_id)
278
   where
279
      device_device.removed is null;
280
 
281
/*
282
   View combines view_device_types, device, and the parent relationship 
283
   (device_device) to give us all the information about one simple device
284
*/
285
create or replace view view_device as 
286
   select
287
      device.device_id,
288
      device.uuid,
289
      device.serial,
290
      device.name device,
291
      device.created device_created,
292
      device.removed device_removed,
293
      view_device_types.device_types,
294
      view_device_types.device_type_ids,
295
      view_device_device.parent_id,
296
      view_device_device.parent_name parent,
297
      view_device_device.created parent_added,
298
      view_device_device.removed parent_removed
299
   from
300
      device
301
      join view_device_types using (device_id) 
302
      left outer join view_device_device using (device_id);
303
 
59 rodolico 304
 
66 rodolico 305
/* 
306
   link owners and locations together for current connection only
307
   Note that even if an owner doesn't have a location, it will still
308
   show up here with a null location
309
*/
59 rodolico 310
create or replace view view_owner_location as
311
   select distinct
312
      owner.name owner,
313
      owner.owner_id,
314
      location.name location,
315
      location.location_id,
316
      owner_location.created,
317
      owner_location.removed
318
   from
319
      owner
66 rodolico 320
      left outer join owner_location using (owner_id)
321
      left outer join location using (location_id)
322
   where
323
      owner_location.removed is null;
324
 
325
/* 
326
   link location to device in such a way as location will still show up
327
   if it has no devices, ie left outer join
328
   will not display historical where location_device is not null (ie, moved)
329
*/
330
create or replace view view_location_device as
331
   select
332
      location.location_id,
333
      location.name location,
334
      location.created location_created,
335
      location.removed location_removed,
336
      view_device.device_id,
337
      view_device.device device,
338
      view_device.device_created,
339
      view_device.device_removed
340
   from 
341
      location 
342
      left outer join location_device using (location_id) 
343
      left outer join view_device using (device_id)
344
   where
345
      location_device.removed is null;
40 rodolico 346
 
66 rodolico 347
/* 
348
   link owner to device in such a way as owner will still show up
349
   if it has no devices, ie left outer join
350
*/
351
create or replace view view_owner_device as
352
   select
353
      owner.owner_id,
354
      owner.name owner,
355
      owner.created owner_created,
356
      owner.removed owner_removed,
357
      view_device.device_id,
358
      view_device.device,
359
      view_device.device_created,
360
      view_device.device_removed
361
   from 
362
      owner 
363
      left outer join owner_device using (owner_id) 
364
      left outer join view_device using (device_id)
365
   where
366
      owner_device.removed is null;
40 rodolico 367
/*
368
   Mongo view that gets all the information together to display
369
   device name, location, owner and type(s)
59 rodolico 370
   NOTE: we are limiting the "part of" to only currently active
371
   relationships
40 rodolico 372
*/
373
 
46 rodolico 374
create or replace view view_device_location_owner_type as
33 rodolico 375
   select
66 rodolico 376
      view_owner_device.device_id device_id,
377
      view_owner_device.device device,
378
      view_owner_device.device_created,
379
      view_owner_device.device_removed,
380
      view_owner_device.owner_id owner_id,
381
      view_owner_device.owner,
382
      view_owner_device.owner_created,
383
      view_owner_device.owner_removed,
384
      view_location_device.location_id location_id,
385
      view_location_device.location,
386
      view_location_device.location_created,
387
      view_location_device.location_removed,
388
      view_owner_location.owner location_owner,
389
      view_owner_location.owner_id location_owner_id,
390
      view_device.parent_id,
391
      view_device.parent,
392
      view_device.device_types
33 rodolico 393
   from
66 rodolico 394
      view_owner_device
67 rodolico 395
      left outer join view_location_device using (device_id)
396
      left outer join view_device using (device_id)
397
      left outer join view_owner_location using (location_id);
36 rodolico 398
 
40 rodolico 399
 
400
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' ) 
401
   on duplicate key update key_value = '0.1';
58 rodolico 402
insert into _system ( key_value, group_name, key_name ) values ( '', 'program', 'motd' );