Subversion Repositories computer_asset_manager_v2

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 rodolico 1
/*
46 rodolico 2
Remove everything wit
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' );
55 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';
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_location_id int unsigned not null auto_increment,
169
   owner_id      int unsigned not null references owner( owner_id ),
170
   location_id        int unsigned not null references location( location_id ),
64 rodolico 171
   created        date default current_timestamp comment 'date record was created',
33 rodolico 172
   removed        date comment 'date record was removed',
46 rodolico 173
   index          location_device ( owner_id,location_id ),
174
   primary key    (owner_location_id)
175
) comment 'links ownership of a location to a owner';   
33 rodolico 176
 
177
/*
178
   Set location of a device. These records are not deleted, but by
179
   setting field removed to non-null value, then creating a new record,
180
   we can track movement of devices.
181
*/
46 rodolico 182
drop table if exists location_device;
183
create table location_device (
184
   location_device_id int unsigned not null auto_increment,
185
   location_id        int unsigned not null references location( location_id ),
33 rodolico 186
   device_id      int unsigned not null references device( device_id ),
64 rodolico 187
   created        date default current_timestamp comment 'date record was created',
33 rodolico 188
   removed        date comment 'date record was removed',
46 rodolico 189
   index          location_device ( location_id,device_id ),
190
   primary key    (location_device_id)
33 rodolico 191
) comment 'links a device to its location';
192
 
193
/*
194
   Set ownership of a device. These records are not deleted, but by
195
   setting field removed to non-null value, then creating a new record,
196
   we can track ownership of devices.
197
*/
46 rodolico 198
drop table if exists owner_device;
199
create table owner_device (
200
   owner_device_id  int unsigned not null auto_increment,
201
   owner_id      int unsigned not null references owner( owner_id ),
33 rodolico 202
   device_id      int unsigned not null references device( device_id ),
64 rodolico 203
   created        date default current_timestamp comment 'date record was created',
33 rodolico 204
   removed        date comment 'date record was removed',
46 rodolico 205
   index          owner_device( owner_id, device_id ),
206
   primary key    ( owner_device_id )
33 rodolico 207
) comment 'links a device to its owner';
208
 
209
/*
210
   There can be a parent/child relationship with devices. For example, a virtual
211
   resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
212
   or, a printer can be attached to a computer, so the printers parent is the
213
   device_id of the computer it is attached to.
214
*/
215
drop table if exists device_device;
216
create table device_device (
217
   device_device_id int unsigned not null auto_increment,
218
   device_id      int unsigned not null references device( device_id ),
219
   parent_id      int unsigned not null references device( device_id ),
64 rodolico 220
   created        date default current_timestamp comment 'date record was created',
33 rodolico 221
   removed        date comment 'date record was removed',
222
   index          device_device( device_id, parent_id ),
223
   primary key    ( device_device_id )
224
) comment 'links a device to another device';
225
 
226
 
40 rodolico 227
/* 
228
   Some views so we don't have to reinvent the wheel when we're trying
229
   to grab some data
230
*/
231
 
232
/*
233
  a simple view that concats the values in device_device_type for 
234
  display. Since mySQL will not allow subqueries in views, required
46 rodolico 235
  to have this information in view_device_location_owner_type
40 rodolico 236
*/
46 rodolico 237
create or replace view view_device_types as
40 rodolico 238
   select 
239
      device_id,
63 rodolico 240
      group_concat(distinct device_type.name) as device_types,
241
      group_concat( distinct device_type.device_type_id) as device_type_ids
40 rodolico 242
   from 
243
      device_device_type 
244
      join device_type using (device_type_id ) 
245
   group by device_id 
246
   order by name;
59 rodolico 247
 
248
/*
249
   View to show relationships between machines, ie the part_of scheme
250
   where one device is actually a "part of" another. Used mainly with
251
   virtualization to show a virtual machine is on a particular 
252
   hypervisor
253
*/
254
create or replace view view_device_device as
255
   select 
256
      device_device_id,
257
      device_device.device_id,
258
      device.name device_name,
259
      device_device.parent_id,
260
      parent.name parent_name,
261
      device_device.created,
262
      device_device.removed 
263
   from 
264
      device 
265
      join device_device using (device_id) 
266
      join device parent on (device_device.parent_id = parent.device_id);
267
 
268
/* link owners and locations together */
269
create or replace view view_owner_location as
270
   select distinct
271
      owner.name owner,
272
      owner.owner_id,
273
      location.name location,
274
      location.location_id,
275
      owner_location.created,
276
      owner_location.removed
277
   from
278
      owner
279
      join owner_location using (owner_id)
280
      join location using (location_id);
40 rodolico 281
 
282
/*
283
   Mongo view that gets all the information together to display
284
   device name, location, owner and type(s)
59 rodolico 285
   NOTE: we are limiting the "part of" to only currently active
286
   relationships
40 rodolico 287
*/
288
 
46 rodolico 289
create or replace view view_device_location_owner_type as
33 rodolico 290
   select
40 rodolico 291
      device.device_id device_id,
292
      device.uuid uuid,
293
      device.serial serial,
33 rodolico 294
      device.name device,
295
      device.created device_created,
296
      device.removed device_removed,
40 rodolico 297
      view_device_types.device_types,
46 rodolico 298
      location.location_id location_id,
299
      location.name location,
300
      location.created location_created,
301
      location.removed location_removed,
63 rodolico 302
      dev_owner.owner_id owner_id,
303
      dev_owner.name owner,
304
      dev_owner.created owner_created,
305
      dev_owner.removed owner_removed,
306
      loc_owner.name location_owner,
307
      loc_owner.owner_id location_owner_id,
308
      loc_owner.created location_owner_created,
309
      loc_owner.removed location_owner_removed,
59 rodolico 310
      parent.parent_id,
311
      parent.parent_name parent,
312
      parent.created parent_created,
313
      parent.removed parent_removed
33 rodolico 314
   from
315
      device
40 rodolico 316
      join view_device_types using (device_id )
63 rodolico 317
      join owner_device using (device_id)
318
      join owner dev_owner on (owner_device.owner_id = dev_owner.owner_id)
46 rodolico 319
      join location_device using (device_id)
320
      join location using (location_id)
63 rodolico 321
      join owner_location on (location.location_id = owner_location.location_id)
322
      join owner loc_owner on (loc_owner.owner_id = owner_location.owner_id)
59 rodolico 323
      left outer join view_device_device parent using (device_id)
324
   where
64 rodolico 325
      parent.removed is null
326
      and owner_device.removed is null
327
      and location_device.removed is null
328
      and owner_location.removed is null;
36 rodolico 329
 
40 rodolico 330
 
331
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' ) 
332
   on duplicate key update key_value = '0.1';
58 rodolico 333
insert into _system ( key_value, group_name, key_name ) values ( '', 'program', 'motd' );