Conditions | 11 |
Total Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like NodeCreate.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python |
||
24 | def run(self, |
||
25 | node, |
||
26 | platform, |
||
27 | ip_address, |
||
28 | engineID, |
||
29 | mon_protocol, |
||
30 | std_community, |
||
31 | community, |
||
32 | status): |
||
33 | """ |
||
34 | Create an node in an Orion monitoring platform. |
||
35 | """ |
||
36 | results = {} |
||
37 | |||
38 | # Sort out which platform & poller to create the node on. |
||
39 | if platform is None: |
||
40 | try: |
||
41 | platform = self.config['defaults']['platform'] |
||
42 | except IndexError: |
||
43 | raise ValueError("No default Orion platform.") |
||
44 | |||
45 | self.logger.info("Connecting to Orion platform: {}".format(platform)) |
||
46 | self.connect(platform) |
||
47 | results['platform'] = platform |
||
48 | |||
49 | # Check node / ip is not already on platform. |
||
50 | self.logger.info( |
||
51 | "Checking node ({}) is not on Orion platform: {}".format(node, |
||
52 | platform)) |
||
53 | |||
54 | # The API allows addition of duplicate nodes, so check and raise |
||
55 | # exception if it's already monitored (by name or IP address). |
||
56 | # FIX ME - Do check of node caption here... |
||
57 | # FIX ME - Do check of ip caption here... |
||
58 | |||
59 | kargs = {'Caption': node, |
||
60 | 'EngineID': engineID, |
||
61 | 'IPAddress': ip_address |
||
62 | } |
||
63 | |||
64 | if mon_protocol == "snmpv2": |
||
65 | kargs['ObjectSubType'] = "SNMP" |
||
66 | kargs['SNMPVersion'] = 2 |
||
67 | |||
68 | if community is not None: |
||
69 | kargs['Community'] = community |
||
70 | elif std_community is not None: |
||
71 | kargs['Community'] = self.config['defaults']['snmp'][std_community] |
||
72 | elif std_community is None: |
||
73 | raise ValueError("Need one of community or std_community") |
||
74 | |||
75 | self.logger.info("Creating Orion Node: {}".format(kargs)) |
||
76 | orion_data = self.create('Orion.Nodes', **kargs) |
||
77 | |||
78 | node_id = re.search('(\d+)$', orion_data).group(0) |
||
79 | results['node_id'] = node_id |
||
80 | |||
81 | self.logger.info("Created Orion Node: {}".format(results['node_id'])) |
||
82 | |||
83 | pollers_to_add = { |
||
84 | 'N.Details.SNMP.Generic': True, |
||
85 | 'N.Uptime.SNMP.Generic': True, |
||
86 | 'N.Cpu.SNMP.HrProcessorLoad': True, |
||
87 | 'N.Memory.SNMP.NetSnmpReal': True, |
||
88 | 'N.AssetInventory.Snmp.Generic': True, |
||
89 | 'N.Topology_Layer3.SNMP.ipNetToMedia': False, |
||
90 | 'N.Routing.SNMP.Ipv4CidrRoutingTable': False |
||
91 | } |
||
92 | |||
93 | if status == 'icmp': |
||
94 | pollers_to_add['N.Status.ICMP.Native'] = True |
||
95 | pollers_to_add['N.Status.SNMP.Native'] = False |
||
96 | pollers_to_add['N.ResponseTime.ICMP.Native'] = True |
||
97 | pollers_to_add['N.ResponseTime.SNMP.Native'] = False |
||
98 | elif status == 'snmp': |
||
99 | pollers_to_add['N.Status.ICMP.Native'] = False |
||
100 | pollers_to_add['N.Status.SNMP.Native'] = True |
||
101 | pollers_to_add['N.ResponseTime.ICMP.Native'] = False |
||
102 | pollers_to_add['N.ResponseTime.SNMP.Native'] = True |
||
103 | |||
104 | pollers = [] |
||
105 | for p in pollers_to_add: |
||
106 | pollers.append({ |
||
107 | 'PollerType': p, |
||
108 | 'NetObject': 'N:{}'.format(node_id), |
||
109 | 'NetObjectType': 'N', |
||
110 | 'NetObjectID': node_id, |
||
111 | 'Enabled': pollers_to_add[p] |
||
112 | }) |
||
113 | |||
114 | for poller in pollers: |
||
115 | # self.logger.info( |
||
116 | # "Adding poller type: {} with status {}... ".format( |
||
117 | # poller['PollerType'], poller['Enabled']) |
||
118 | # ) |
||
119 | response = self.create('Orion.Pollers', **poller) |
||
120 | # self.logger.info("Done: {}".format(response)) |
||
121 | |||
122 | return results |
||
123 |