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 _config;
4
drop table if exists _menu;
5
drop table if exists _permissions;
6
drop table if exists _permissions_categories;
7
drop table if exists _system;
8
drop table if exists _users_permissions;
9
drop view if exists _view_users_permissions;
10
drop table if exists client;
11
drop table if exists client_device;
12
drop table if exists client_site;
13
drop table if exists device;
14
drop table if exists device_device;
15
drop table if exists device_device_type;
16
drop table if exists device_type;
17
drop table if exists site;
18
drop table if exists site_device;
19
drop table if exists temp;
20
drop view if exists view_device_types;
21
drop view if exists view_device_site_client_type;
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
*/
79
insert into camp2._menu values (null,null,'Home', '/index.php', 'menu_home' );
68 rodolico 80
insert into camp2._menu select null,_menu_id,'Owners', '/index.php?module=Owner', 'menu_owner' from camp2._menu where caption = 'Home';
81
insert into camp2._menu select null,_menu_id,'Locations', '/index.php?module=Location', 'menu_location' from camp2._menu where caption = 'Home';
82
insert into camp2._menu select null,_menu_id,'Devices', '/index.php?module=Device', 'menu_device' from camp2._menu where caption = 'Home';
55 rodolico 83
insert into camp2._menu values (null,null,'Reports', '/index.php?module=report', 'menu_report' );
33 rodolico 84
 
85
/*
86
   simple table to hold ownership information
87
*/
46 rodolico 88
drop table if exists owner;
89
create table owner (
90
   owner_id       int unsigned not null auto_increment,
91
   name           varchar(64) comment 'name of owner',
63 rodolico 92
   uuid           varchar(36) comment 'unique id of this owner, normally uuid',
64 rodolico 93
   created        date default current_timestamp comment 'date record was created',
33 rodolico 94
   removed        date comment 'date record was removed',
46 rodolico 95
   primary key    (owner_id)
96
) comment 'hold owner information';
33 rodolico 97
 
98
/*
46 rodolico 99
   simple table to hold location where a device is located
33 rodolico 100
*/
46 rodolico 101
drop table if exists location;
102
create table location (
103
   location_id    int unsigned not null auto_increment,
104
   name           varchar(64) comment 'name of location',
63 rodolico 105
   uuid           varchar(36) comment 'unique id of this location, normally uuid',
64 rodolico 106
   created        date default current_timestamp comment 'date record was created',
33 rodolico 107
   removed        date comment 'date record was removed',
46 rodolico 108
   primary key    (location_id)
109
) comment 'hold location information';
33 rodolico 110
 
111
/*
112
   table which holds a device type, such as server, workstation
113
   printer, virtual, etc...
114
*/
115
drop table if exists device_type;
116
create table device_type (
117
   device_type_id int unsigned not null auto_increment,
118
   name           varchar(64) comment 'name of device type',
64 rodolico 119
   created        date default current_timestamp comment 'date record was created',
33 rodolico 120
   removed        date comment 'date record was removed',
121
   primary key    (device_type_id)
122
) comment 'simple child table to determine the type of device we have';
123
 
124
/*
40 rodolico 125
   holds very basic information on a device such as its name and a unique id.
126
   This is the main table for the database, and each device
33 rodolico 127
   should be uniquely identified. We will allow name to be modified
128
   randomly, however.
129
 
130
   Internally, we find this device based on device_id, but for remote
131
   systems, we use the combined uuid and serial to uniquely identify
132
   some manufacturers use one uuid for all systems, but the uuid and serial
133
   number combination should be unique. serial can be any arbitrary string
134
   and it is suggested to use manufacturer:serial or something like that
135
*/
136
drop table if exists device;
137
create table device (
138
   device_id      int unsigned not null auto_increment,
40 rodolico 139
   uuid           varchar(36) comment 'unique id of this device, normally uuid',
33 rodolico 140
   serial         varchar(32) comment 'serial number of this device, if we have it',
141
   name           varchar(64) comment 'name of device',
64 rodolico 142
   created        date default current_timestamp comment 'date record was created',
33 rodolico 143
   removed        date comment 'date record was removed',
40 rodolico 144
   unique key     unique_uuid( uuid, serial ),
33 rodolico 145
   primary key    (device_id)
146
) comment 'holds individual devices';
147
 
40 rodolico 148
/*
149
   Many to many join table allowing devices to have multiple device
150
   types. NOTE: I'm using device_id and device_type_id as the composite
151
   primary key, so no duplicates, and we don't need an 'id' column
152
*/
33 rodolico 153
 
40 rodolico 154
drop table if exists device_device_type;
155
create table device_device_type (
156
   device_id      int unsigned not null references device( device_id ),
157
   device_type_id int unsigned not null references device_type( device_type_id ),
158
   primary key (device_id,device_type_id)
159
) comment 'many to many join for device and device_type tables';
160
 
33 rodolico 161
/*
46 rodolico 162
   Set ownership of a location. These records are not deleted, but by
33 rodolico 163
   setting field removed to non-null value, then creating a new record,
46 rodolico 164
   we can track ownership of locations.
33 rodolico 165
*/
46 rodolico 166
drop table if exists owner_location;
167
create table owner_location (
168
   owner_id      int unsigned not null references owner( owner_id ),
169
   location_id        int unsigned not null references location( location_id ),
64 rodolico 170
   created        date default current_timestamp comment 'date record was created',
33 rodolico 171
   removed        date comment 'date record was removed',
66 rodolico 172
   unique key    (owner_id,location_id, removed)
46 rodolico 173
) comment 'links ownership of a location to a owner';   
33 rodolico 174
 
175
/*
176
   Set location of a device. These records are not deleted, but by
177
   setting field removed to non-null value, then creating a new record,
178
   we can track movement of devices.
179
*/
46 rodolico 180
drop table if exists location_device;
181
create table location_device (
182
   location_id        int unsigned not null references location( location_id ),
33 rodolico 183
   device_id      int unsigned not null references device( device_id ),
64 rodolico 184
   created        date default current_timestamp comment 'date record was created',
33 rodolico 185
   removed        date comment 'date record was removed',
66 rodolico 186
   unique key    (location_id,device_id, removed)
33 rodolico 187
) comment 'links a device to its location';
188
 
189
/*
190
   Set ownership of a device. These records are not deleted, but by
191
   setting field removed to non-null value, then creating a new record,
192
   we can track ownership of devices.
193
*/
46 rodolico 194
drop table if exists owner_device;
195
create table owner_device (
196
   owner_id      int unsigned not null references owner( owner_id ),
33 rodolico 197
   device_id      int unsigned not null references device( device_id ),
64 rodolico 198
   created        date default current_timestamp comment 'date record was created',
66 rodolico 199
   removed        date default null comment 'date record was removed',
200
   unique key    ( owner_id, device_id, removed )
33 rodolico 201
) comment 'links a device to its owner';
202
 
203
/*
204
   There can be a parent/child relationship with devices. For example, a virtual
205
   resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
206
   or, a printer can be attached to a computer, so the printers parent is the
207
   device_id of the computer it is attached to.
208
*/
209
drop table if exists device_device;
210
create table device_device (
211
   device_id      int unsigned not null references device( device_id ),
212
   parent_id      int unsigned not null references device( device_id ),
64 rodolico 213
   created        date default current_timestamp comment 'date record was created',
66 rodolico 214
   removed        date default null comment 'date record was removed',
215
   unique key    ( device_id, parent_id, removed )
33 rodolico 216
) comment 'links a device to another device';
217
 
218
 
40 rodolico 219
/* 
220
   Some views so we don't have to reinvent the wheel when we're trying
221
   to grab some data
222
*/
223
 
224
/*
225
  a simple view that concats the values in device_device_type for 
226
  display. Since mySQL will not allow subqueries in views, required
46 rodolico 227
  to have this information in view_device_location_owner_type
40 rodolico 228
*/
46 rodolico 229
create or replace view view_device_types as
40 rodolico 230
   select 
231
      device_id,
63 rodolico 232
      group_concat(distinct device_type.name) as device_types,
233
      group_concat( distinct device_type.device_type_id) as device_type_ids
40 rodolico 234
   from 
235
      device_device_type 
236
      join device_type using (device_type_id ) 
237
   group by device_id 
238
   order by name;
59 rodolico 239
 
240
/*
241
   View to show relationships between machines, ie the part_of scheme
242
   where one device is actually a "part of" another. Used mainly with
243
   virtualization to show a virtual machine is on a particular 
244
   hypervisor
245
*/
246
create or replace view view_device_device as
247
   select 
248
      device_device.device_id,
249
      device.name device_name,
250
      device_device.parent_id,
251
      parent.name parent_name,
252
      device_device.created,
253
      device_device.removed 
254
   from 
255
      device 
256
      join device_device using (device_id) 
66 rodolico 257
      join device parent on (device_device.parent_id = parent.device_id)
258
   where
259
      device_device.removed is null;
260
 
261
/*
262
   View combines view_device_types, device, and the parent relationship 
263
   (device_device) to give us all the information about one simple device
264
*/
265
create or replace view view_device as 
266
   select
267
      device.device_id,
268
      device.uuid,
269
      device.serial,
270
      device.name device,
271
      device.created device_created,
272
      device.removed device_removed,
273
      view_device_types.device_types,
274
      view_device_types.device_type_ids,
275
      view_device_device.parent_id,
276
      view_device_device.parent_name parent,
277
      view_device_device.created parent_added,
278
      view_device_device.removed parent_removed
279
   from
280
      device
281
      join view_device_types using (device_id) 
282
      left outer join view_device_device using (device_id);
283
 
59 rodolico 284
 
66 rodolico 285
/* 
286
   link owners and locations together for current connection only
287
   Note that even if an owner doesn't have a location, it will still
288
   show up here with a null location
289
*/
59 rodolico 290
create or replace view view_owner_location as
291
   select distinct
292
      owner.name owner,
293
      owner.owner_id,
294
      location.name location,
295
      location.location_id,
296
      owner_location.created,
297
      owner_location.removed
298
   from
299
      owner
66 rodolico 300
      left outer join owner_location using (owner_id)
301
      left outer join location using (location_id)
302
   where
303
      owner_location.removed is null;
304
 
305
/* 
306
   link location to device in such a way as location will still show up
307
   if it has no devices, ie left outer join
308
   will not display historical where location_device is not null (ie, moved)
309
*/
310
create or replace view view_location_device as
311
   select
312
      location.location_id,
313
      location.name location,
314
      location.created location_created,
315
      location.removed location_removed,
316
      view_device.device_id,
317
      view_device.device device,
318
      view_device.device_created,
319
      view_device.device_removed
320
   from 
321
      location 
322
      left outer join location_device using (location_id) 
323
      left outer join view_device using (device_id)
324
   where
325
      location_device.removed is null;
40 rodolico 326
 
66 rodolico 327
/* 
328
   link owner to device in such a way as owner will still show up
329
   if it has no devices, ie left outer join
330
*/
331
create or replace view view_owner_device as
332
   select
333
      owner.owner_id,
334
      owner.name owner,
335
      owner.created owner_created,
336
      owner.removed owner_removed,
337
      view_device.device_id,
338
      view_device.device,
339
      view_device.device_created,
340
      view_device.device_removed
341
   from 
342
      owner 
343
      left outer join owner_device using (owner_id) 
344
      left outer join view_device using (device_id)
345
   where
346
      owner_device.removed is null;
40 rodolico 347
/*
348
   Mongo view that gets all the information together to display
349
   device name, location, owner and type(s)
59 rodolico 350
   NOTE: we are limiting the "part of" to only currently active
351
   relationships
40 rodolico 352
*/
353
 
46 rodolico 354
create or replace view view_device_location_owner_type as
33 rodolico 355
   select
66 rodolico 356
      view_owner_device.device_id device_id,
357
      view_owner_device.device device,
358
      view_owner_device.device_created,
359
      view_owner_device.device_removed,
360
      view_owner_device.owner_id owner_id,
361
      view_owner_device.owner,
362
      view_owner_device.owner_created,
363
      view_owner_device.owner_removed,
364
      view_location_device.location_id location_id,
365
      view_location_device.location,
366
      view_location_device.location_created,
367
      view_location_device.location_removed,
368
      view_owner_location.owner location_owner,
369
      view_owner_location.owner_id location_owner_id,
370
      view_device.parent_id,
371
      view_device.parent,
372
      view_device.device_types
33 rodolico 373
   from
66 rodolico 374
      view_owner_device
67 rodolico 375
      left outer join view_location_device using (device_id)
376
      left outer join view_device using (device_id)
377
      left outer join view_owner_location using (location_id);
36 rodolico 378
 
40 rodolico 379
 
380
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' ) 
381
   on duplicate key update key_value = '0.1';
58 rodolico 382
insert into _system ( key_value, group_name, key_name ) values ( '', 'program', 'motd' );