-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCofCHostObject.py
173 lines (152 loc) · 6.56 KB
/
CofCHostObject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import constants
from HostObject import HostObject
import re
"""
Austin Hunt, Sept 18, 2019
Module for adapting the generic bulk object creator to CofC's network
Subclass of CofCHost object, but add a method that dives deeper into variable definitions by
parsing hostname
OZ-App/Service-Detail
"""
class CofCHostObject(HostObject):
def __init__(self, address=None, hostname=None, notes=None, os=None, display_name=None):
#super().__init__(self) # keep the inheritance of superclass
super(CofCHostObject,self).__init__(address,hostname,notes,os,display_name)
# New variables custom to CofC
self.networkzone_def = None
self.year_def = None
self.service_def = None
self.detail_def = None
self.temp_def = "vars.temporary = [\"disable ssh\", \"disable notifications\"]"
self.role_mappings = {
'DB': 'Database',
'SBX': 'Sandbox',
'APP': 'Application',
# 'ICX':'Java Web Container', # Correct?
'MQ': 'RabbitMQ',
'WFL': 'Workflow',
# 'ERP': 'Enterprise Resource Planning',
'ESM': 'Ellucian Solution Manager',
'SQL': 'MySQL',
'MW': 'Middleware',
'WEB': 'Web Server',
'SA': 'Icinga Satellite',
'M0': 'Master'
}
self.os_mappings = {
'R': 'Red Hat',
'W': 'Windows',
'U': 'Ubuntu',
'C': 'CentOS',
'V': 'Virtual Appliance',
'F': 'Fedora',
'P': 'PAN-OS',
'L': 'Linux',
}
self.zone_mappings = {
'M': 'Management',
'P': 'Production',
'T': 'Test',
'D': 'Development'
}
# Can only use host name to set variables if hostname matches the naming scheme (YYOZ-A/S-DDDDD); define instance variable for pattern
self.name_scheme_pattern = r'^(([0-9]{2})?(([A-Z]{2})|([a-z]{2})))\-[0-9A-Za-z]+\-[a-zA-Z0-9]+(\.(guest\.vm\.)?cougars\.int)?$'
# Pattern to match beginning YYOZ pattern vs just beginning OZ pattern
self.year_oz_pattern = r'^[0-9]{2}(([A-Z]{2})|([a-z]{2}))\-.*'
# Pattern to match just beginning OZ pattern
# regex pattern matching any XX-yyyyyy... naßme
self.oz_pattern = r'^(([A-Z]{2})|([a-z]{2}))\-.*'
self.set_vars()
# return boolean representing whether or not host name matches the cofc naming scheme
def matches_name_scheme(self):
return True if re.match(self.name_scheme_pattern, self.hostname) else False
# method that sets hostname-based vars if naming scheme is matched
def set_vars(self):
if self.matches_name_scheme():
if self.os_def == None:
# Check if superclass set this already
self.set_os() # calls the set_year method internally if included
self.set_networkzone()
self.set_service()
self.set_detail()
# Setter methods
# method to set the os_def variable
def set_os(self):
# First check the year_oz_pattern other wise oz_pattern will match inaccurately if year_oz_pattern is present
if re.search(self.year_oz_pattern, self.hostname): # can proceed with defining os
self.os_def = "vars.os = \"" + \
self.get_os(True) + "\"" if self.get_os(True) is not None else ""
# Set the year_def
self.set_year()
elif re.search(self.oz_pattern, self.hostname):
self.os_def = "vars.os = \"" + \
self.get_os(False) + "\"" if self.get_os(False) is not None else ""
# Method to set the year_def variable
def set_year(self):
self.year_def = "vars.purchase_year = \"" + self.get_year() + "\""
# Method to set the networkzone_def variable
def set_networkzone(self):
self.networkzone_def = "vars.network_zone = \"" + self.get_networkzone() + "\""
# Method to set the service_def variable
def set_service(self):
self.service_def = "vars.service = \"" + self.get_service() + "\""
def set_detail(self):
detailstr = self.get_detail()
self.detail_def = "vars.detail = \"" + detailstr + "\"" if detailstr != "" else None
# Helper getter methods
# helper method that takes the hostname and outputs the os based on the first letter
def get_os(self, includes_year):
try:
os_char = self.hostname[2] if includes_year else self.hostname[0]
res = self.os_mappings[os_char.upper()]
except Exception as e:
print(e)
print("For host:",self.hostname)
return res
# Outputs the year based on first two characters of hostname
def get_year(self):
return "20" + self.hostname[:2]
# Outputs the network zone based on last character of first section of host name
def get_networkzone(self):
# last character of first section before - character
zone_char = self.hostname.split("-")[0][-1]
return self.zone_mappings[zone_char.upper()]
# Outputs the service based on middle section of hostname
def get_service(self):
return self.hostname.split("-")[1]
def get_detail(self):
# this one's fun. lots of different strings can be found here.
# first, address names ending with a number, indicating an indexer; assume a max string of 3 digits for indexing.
# use regex to get that number
indexer = self.get_indexer()
# check for specific strings indicating role
role = self.get_role()
rolestr = "role = " + role + "; " if role is not None else ""
return (indexer + rolestr)
def get_indexer(self):
#print("Getting index...")
end_num_pattern = r'([0-9]{1,3})((\.guest\.vm)?\.cougars\.int)?$'
m1 = re.search(end_num_pattern, self.hostname)
indexer = ""
if m1:
#print("Matched an index")
# trim out terminating domain
end_num_pattern = r'([0-9]{1,3})$'
num = re.search(end_num_pattern,m1.group()).group()
# to be included as part of the detail
indexer = "indexer = " + str(num) + "; "
else:
#print("No indexer...")
pass
return indexer
def get_role(self):
# from last section of hostname, get the string until numbers start
role = ""
for c in self.hostname.split("-")[-1]:
if c.isdigit():
break
role += c
try:
return self.role_mappings[role.upper()]
except:
return None