SHOW:
|
|
- or go back to the newest paste.
| 1 | /* | |
| 2 | - | Name: Create Database Virtual Server |
| 2 | + | Name: Create MSSQL Connection Relationship |
| 3 | - | Table: Windows Cluster Resource |
| 3 | + | Table: TCP Connection |
| 4 | Order: 100 | |
| 5 | When: after | |
| 6 | Insert: true | |
| 7 | Update: true | |
| 8 | - | Condition: current.resource_type == 'IP Address' |
| 8 | + | Condition: current.to_port == 1433 |
| 9 | */ | |
| 10 | ||
| 11 | - | if(current.name.indexOf('SQL IP Address') == 0) {
|
| 11 | + | createMSSQLRel(); |
| 12 | - | createDBVirtualServer(current); |
| 12 | + | |
| 13 | function createMSSQLRel() {
| |
| 14 | //Check for matching MSSQL Instances by IP | |
| 15 | - | function createDBVirtualServer(clusterRes) {
|
| 15 | + | var MSSQLInst = new GlideRecord("cmdb_ci_db_mssql_instance");
|
| 16 | - | var clusterVIP = new GlideRecord('cmdb_ci_cluster_vip');
|
| 16 | + | MSSQLInst.addQuery("ip_address", current.to_ip);
|
| 17 | - | clusterVIP.addQuery('name', clusterRes.name);
|
| 17 | + | MSSQLInst.query(); |
| 18 | - | clusterVIP.addQuery('cluster', clusterRes.cluster);
|
| 18 | + | if(MSSQLInst.next()) {
|
| 19 | - | clusterVIP.query(); |
| 19 | + | //If not set to absent or if absent for less than 30 days, create new. Otherwise delete relationship |
| 20 | - | while(clusterVIP.next()) {
|
| 20 | + | if(current.absent == false || (current.absent == true && current.sys_updated_on < gs.daysAgo(-30))){
|
| 21 | - | //gs.log(clusterVIP.name + '/' + clusterVIP.ip_address + '/' + clusterVIP.cluster.name, 'eric'); |
| 21 | + | createRelationship(current.computer.sys_id, MSSQLInst.sys_id); |
| 22 | - | var VIPServer = clusterVIP.name.split('(')[1].replace(')', '');//isolate the virtual server name
|
| 22 | + | }else{
|
| 23 | - | //Create MSSQL Database Virtual Server |
| 23 | + | deleteRelationship(current.computer.sys_id, MSSQLInst.sys_id); |
| 24 | - | var DBVirtualServer = new GlideRecord('cmdb_ci_database');
|
| 24 | + | |
| 25 | - | DBVirtualServer.addQuery('name', VIPServer);
|
| 25 | + | |
| 26 | - | DBVirtualServer.query(); |
| 26 | + | |
| 27 | - | if(!DBVirtualServer.next()) {
|
| 27 | + | |
| 28 | - | DBVirtualServer.initialize(); |
| 28 | + | function createRelationship(source,dest) {
|
| 29 | - | DBVirtualServer.name = VIPServer; |
| 29 | + | var computer = new GlideRecord("cmdb_ci_computer");
|
| 30 | - | DBVirtualServer.ip_address = clusterVIP.ip_address; |
| 30 | + | computer.addQuery("sys_id", source);
|
| 31 | - | DBVirtualServer.type = 'Microsoft SQL Server'; |
| 31 | + | computer.query(); |
| 32 | - | DBVirtualServer.u_substatus = 'active'; |
| 32 | + | if(computer.next()) {
|
| 33 | - | DBVirtualServer.short_description = gs.nowDateTime() + ' - Auto created from cluster SQL IP Address resource'; |
| 33 | + | var rel1 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
|
| 34 | - | DBVirtualServer.insert(); |
| 34 | + | rel1.addQuery('child.sys_id', source);
|
| 35 | rel1.addQuery('parent.sys_id', dest);
| |
| 36 | - | //create MSSQL instance |
| 36 | + | rel1.query(); |
| 37 | - | var DBInstance = new GlideRecord('cmdb_ci_db_mssql_instance');
|
| 37 | + | if(!rel1.next()) {
|
| 38 | - | DBInstance.addQuery('name', 'MSSQLSERVER@' + VIPServer + '(Microsoft SQL Server)');
|
| 38 | + | var newRel = new GlideRecord('cmdb_rel_ci');//create relationship with type: Serves MSSQL Data::Queries MSSQL Data
|
| 39 | - | DBInstance.query(); |
| 39 | + | newRel.initalize(); |
| 40 | - | if(!DBInstance.next()) {
|
| 40 | + | newRel.parent = dest; |
| 41 | - | DBInstance.initialize(); |
| 41 | + | newRel.child = source; |
| 42 | - | DBInstance.name = 'MSSQLSERVER@' + VIPServer + '(Microsoft SQL Server)'; |
| 42 | + | newRel.type = 'baa92dcb74906000ce885c6f8d102bf4'; |
| 43 | - | DBInstance.ip_address = clusterVIP.ip_address; |
| 43 | + | newRel.insert(); |
| 44 | - | DBInstance.u_substatus = 'active'; |
| 44 | + | |
| 45 | - | DBInstance.short_description = gs.nowDateTime() + ' - Auto created from cluster SQL IP Address resource'; |
| 45 | + | |
| 46 | - | DBInstance.install_date = gs.nowDateTime(); |
| 46 | + | |
| 47 | - | DBInstance.insert(); |
| 47 | + | |
| 48 | function deleteRelationship(source,dest) {
| |
| 49 | - | //Create DB Virtual Server -> Cluster relationship |
| 49 | + | var computer = new GlideRecord("cmdb_ci_computer");
|
| 50 | - | var clusterRel = new GlideRecord('cmdb_rel_ci');//create relationship with type: Runs on::Runs
|
| 50 | + | computer.addQuery("sys_id", source);
|
| 51 | - | clusterRel.addQuery('parent', clusterVIP.cluster.sys_id);
|
| 51 | + | computer.query(); |
| 52 | - | clusterRel.addQuery('child', DBVirtualServer.sys_id);
|
| 52 | + | if(computer.next()) {
|
| 53 | - | clusterRel.addQuery('type', '60bc4e22c0a8010e01f074cbe6bd73c3');
|
| 53 | + | var rel1 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
|
| 54 | - | clusterRel.query(); |
| 54 | + | rel1.addQuery('child.sys_id', source);
|
| 55 | - | if(!clusterRel.next()){
|
| 55 | + | rel1.addQuery('parent.sys_id', dest);
|
| 56 | - | clusterRel.initalize(); |
| 56 | + | rel1.addQuery('type', 'baa92dcb74906000ce885c6f8d102bf4')
|
| 57 | - | clusterRel.parent = clusterVIP.cluster.sys_id; |
| 57 | + | rel1.addQuery('sys_updated_on', '>', gs.daysAgo(-30))//if over 30 days old
|
| 58 | - | clusterRel.child = DBVirtualServer.sys_id; |
| 58 | + | rel1.query(); |
| 59 | - | clusterRel.type = '60bc4e22c0a8010e01f074cbe6bd73c3'; |
| 59 | + | if(rel1.next()) {
|
| 60 | - | clusterRel.insert(); |
| 60 | + | rel1.deleteRecord(); |
| 61 | } | |
| 62 | - | |
| 62 | + | |
| 63 | - | //Create DB Virtual Server -> MSSQL instance relationship |
| 63 | + |