sidetrak

Create MSSQL Connection Relationship

Apr 30th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Name: Create MSSQL Connection Relationship
  3. Table: TCP Connection
  4. Order: 100
  5. When: after
  6. Insert: true
  7. Update: true
  8. Condition: current.to_port == 1433
  9. */
  10.  
  11. createMSSQLRel();
  12.  
  13. function createMSSQLRel() {
  14.    //Check for matching MSSQL Instances by IP
  15.    var MSSQLInst = new GlideRecord("cmdb_ci_db_mssql_instance");
  16.    MSSQLInst.addQuery("ip_address", current.to_ip);
  17.    MSSQLInst.query();
  18.    if(MSSQLInst.next()) {
  19.       //If not set to absent or if absent for less than 30 days, create new. Otherwise delete relationship
  20.       if(current.absent == false || (current.absent == true && current.sys_updated_on < gs.daysAgo(-30))){
  21.          createRelationship(current.computer.sys_id, MSSQLInst.sys_id);
  22.       }else{
  23.          deleteRelationship(current.computer.sys_id, MSSQLInst.sys_id);
  24.       }
  25.    }
  26. }
  27.  
  28. function createRelationship(source,dest) {
  29.    var computer = new GlideRecord("cmdb_ci_computer");
  30.    computer.addQuery("sys_id", source);
  31.    computer.query();
  32.    if(computer.next()) {
  33.       var rel1 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
  34.       rel1.addQuery('child.sys_id', source);
  35.       rel1.addQuery('parent.sys_id', dest);
  36.       rel1.query();
  37.       if(!rel1.next()) {
  38.          var newRel = new GlideRecord('cmdb_rel_ci');//create relationship with type: Serves MSSQL Data::Queries MSSQL Data
  39.          newRel.initalize();
  40.          newRel.parent = dest;
  41.          newRel.child = source;
  42.          newRel.type = 'baa92dcb74906000ce885c6f8d102bf4';
  43.          newRel.insert();
  44.       }
  45.    }
  46. }
  47.  
  48. function deleteRelationship(source,dest) {
  49.    var computer = new GlideRecord("cmdb_ci_computer");
  50.    computer.addQuery("sys_id", source);
  51.    computer.query();
  52.    if(computer.next()) {
  53.       var rel1 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
  54.       rel1.addQuery('child.sys_id', source);
  55.       rel1.addQuery('parent.sys_id', dest);
  56.       rel1.addQuery('type', 'baa92dcb74906000ce885c6f8d102bf4')
  57.       rel1.addQuery('sys_updated_on', '>', gs.daysAgo(-30))//if over 30 days old
  58.       rel1.query();
  59.       if(rel1.next()) {
  60.          rel1.deleteRecord();
  61.       }
  62.    }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment