add pcu_name to pcufailed_notice
[monitor.git] / monitor / database / info / findbad.py
1 from elixir import Entity, Field, OneToMany, ManyToOne, ManyToMany
2 from elixir import options_defaults, using_options, setup_all, belongs_to
3 from elixir import String, Integer as Int, DateTime, PickleType, Boolean
4 from datetime import datetime,timedelta
5 import elixir
6 import traceback
7 from elixir.ext.versioned import *
8 from pcucontrol import reboot
9
10 from monitor.database.dborm import mon_metadata, mon_session
11 __metadata__ = mon_metadata
12 __session__  = mon_session
13
14
15 class FindbadNodeRecord(Entity):
16         @classmethod
17         def get_all_latest(cls):
18                 return cls.query.all()
19
20         @classmethod
21         def get_latest_by(cls, **kwargs):
22                 return cls.query.filter_by(**kwargs).first()
23
24         @classmethod
25         def get_latest_by(cls, **kwargs):
26                 return cls.query.filter_by(**kwargs).first()
27
28         @classmethod
29         def get_latest_n_by(cls, n=3, **kwargs):
30                 return cls.query.filter_by(**kwargs)
31
32 # ACCOUNTING
33         date_checked = Field(DateTime,default=datetime.now)
34         round = Field(Int,default=0)
35         hostname = Field(String,primary_key=True,default=None)
36         loginbase = Field(String)
37
38 # INTERNAL
39         kernel_version = Field(String,default=None)
40         bootcd_version = Field(String,default=None)
41         nm_status = Field(String,default=None)
42         fs_status = Field(String,default=None)
43         dns_status = Field(String,default=None)
44         external_dns_status = Field(Boolean,default=True)
45         uptime = Field(String,default=None)     
46         rpms = Field(String,default=None)       
47         princeton_comon_dir = Field(Boolean,default=False)
48         princeton_comon_running = Field(Boolean,default=False)
49         princeton_comon_procs = Field(Int,default=None)
50
51 # EXTERNAL
52         plc_node_stats = Field(PickleType,default=None)
53         plc_site_stats = Field(PickleType,default=None)
54         plc_pcuid      = Field(Int,default=None)
55         comon_stats    = Field(PickleType,default=None)
56         port_status    = Field(PickleType,default=None)
57         firewall                = Field(Boolean,default=False)
58         ssh_portused = Field(Int,default=22)
59         ssh_status = Field(Boolean,default=False)
60         ssh_error = Field(String,default=None)  # set if ssh_access == False
61         traceroute = Field(String,default=None) 
62         ping_status = Field(Boolean,default=False)
63
64 # INFERRED
65         observed_category = Field(String,default=None)
66         observed_status = Field(String,default=None)
67
68         acts_as_versioned(ignore=['date_checked'])
69         # NOTE: this is the child relation
70         #action = ManyToOne('ActionRecord', required=False)
71
72 class FindbadPCURecord(Entity):
73         @classmethod
74         def get_all_latest(cls):
75                 return cls.query.all()
76
77         @classmethod
78         def get_latest_by(cls, **kwargs):
79                 return cls.query.filter_by(**kwargs).first()
80
81         def pcu_name(self):
82                 if self.plc_pcu_stats['hostname'] is not None and self.plc_pcu_stats['hostname'] is not "":
83                         return self.plc_pcu_stats['hostname']
84                 elif self.plc_pcu_stats['ip'] is not None and self.plc_pcu_stats['ip'] is not "":
85                         return self.plc_pcu_stats['ip']
86                 else:
87                         return None
88
89         def format_ports(self):
90                 retval = []
91                 filtered_length=0
92
93                 supported_ports=reboot.model_to_object(self.plc_pcu_stats['model']).supported_ports
94                 data = self.port_status.copy()
95
96                 if data and len(data.keys()) > 0 :
97                         for port in supported_ports:
98                                 try:
99                                         state = data[str(port)]
100                                 except:
101                                         state = "unknown"
102
103                                 if state == "filtered":
104                                         filtered_length += 1
105                                         
106                                 retval.append( (port, state) )
107
108                 if retval == []: 
109                         retval = [( "Closed/Filtered", "" )]
110
111                 if filtered_length == len(supported_ports):
112                         retval = [( "All Filtered", "" )]
113
114                 return retval
115
116         def format_pcu_shortstatus(self):
117                 status = "error"
118                 if self.reboot_trial_status:
119                         if self.reboot_trial_status == str(0):
120                                 status = "Ok"
121                         elif self.reboot_trial_status == "NetDown" or self.reboot_trial_status == "Not_Run":
122                                 status = self.reboot_trial_status
123                         else:
124                                 status = "error"
125
126                 return status
127
128         def test_is_ok(self):
129                 if self.reboot_trial_status == str(0):
130                         return True
131                 else:
132                         return False
133
134         def pcu_errors(self):
135                 message = "\n"
136                 message += "\tModel: %s\n" % self.plc_pcu_stats['model']
137                 message += "\tMissing Fields: %s\n" % ( self.entry_complete == "" and "None missing" or self.entry_complete )
138                 message += "\tDNS Status: %s\n" % self.dns_status
139                 message += "\tPort Status: %s\n" % self.format_ports()
140                 message += "\tTest Results: %s\n" % self.format_pcu_shortstatus()
141                 return message
142
143 # ACCOUNTING
144         date_checked = Field(DateTime)
145         round = Field(Int,default=0)
146         plc_pcuid = Field(Int)
147
148 # EXTERNAL
149         plc_pcu_stats = Field(PickleType,default=None)
150         dns_status = Field(String)
151         port_status = Field(PickleType)
152         entry_complete = Field(String)
153
154 # INTERNAL
155 # INFERRED
156         reboot_trial_status = Field(String)
157
158         acts_as_versioned(ignore=['date_checked'])