· 10 years ago · Aug 23, 2016, 06:46 AM
1<?php
2/*
3Description: VM Control tool provides a solution for monitoring & tracking multiple virtual machines based web appliction.
4Languages: PHP,CSS,HTML,SQL SERVER
5Supported: Chorme 36.xxx only
6Version: 2.5
7Created by: Oren Zamir 07/2016
8*/
9
10// Import files
11require_once ('php/Sqlserver.class.php');
12require_once ('view/Machine.class.php');
13require_once ('view/Monitor.class.php');
14
15class Model{
16
17 // Set tables on variables
18 private $panelControlScripts = '';
19 private $panelControlVmControl = '';
20 private $panelControlVmControlMonitor = '';
21 private $panelControlScriptRunResults = '';
22 private $pensionProdScriptStepResults = '';
23
24 public function __construct()
25 {
26 // Create db object
27 $this->db = new Sqlserver();
28 }
29
30 /*************************************************
31 FUNCTION: getMachinesList
32 PURPOSE: Get machines array from cookie by environment
33 GET VALUES: $environment, $cookie
34 RETURN VALUES: $row
35 *************************************************/
36 public function getMachinesList($environment, $cookie)
37 {
38 // Start query
39 $stmt = $this->db->query("SELECT * from $this->panelControlVmControl Where Environment = '$environment' $cookie");
40 $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
41
42 // Frees all resources associated
43 sqlsrv_free_stmt($stmt);
44
45 // Return machine array
46 return $row;
47 }
48
49 /*************************************************
50 FUNCTION: getColumnRows
51 PURPOSE: Return column Rows details
52 GET VALUES: $column
53 RETURN VALUES: $resultbycolumn
54 *************************************************/
55 public function getColumnRows($column)
56 {
57 // Start query (call returnRowsFromDb function)
58 $resultbycolumn = $this->db->returnRowsFromDb("SELECT DISTINCT $column FROM $this->panelControlVmControl","$column");
59
60 // Return rows by column
61 return $resultbycolumn;
62 }
63
64 /*************************************************
65 FUNCTION: getCurrentPk
66 PURPOSE: Return current pk from database
67 GET VALUES: $machineName, $status
68 RETURN VALUES: FINISHED or $pensionProdPk or $expectedTime
69 *************************************************/
70 public function getCurrentPk($machineName, $status)
71 {
72 // When machine is free
73 if ( $status == 0 ){
74 return FREE;
75 }
76
77 // Check if return PK from panel control between 7 min
78 $stmt = $this->db->query("SELECT TOP 1 PK FROM $this->panelControlScriptRunResults Where machineName = '$machineName' AND RunDate >= DateADD(mi, -7, CURRENT_TIMESTAMP) order by RunDate desc");
79 $expectedTime = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
80
81 // Frees all resources associated
82 sqlsrv_free_stmt($stmt);
83
84 // When PK Clounm on panel control is empty after 7 min
85 if ($expectedTime['PK'] == ""){
86 // Save the last pk on panel control
87 $stmt = $this->db->query("SELECT TOP 1 PK FROM $this->panelControlScriptRunResults Where machineName = '$machineName' order by RunDate desc");
88 $panelControlPk = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
89
90 // Frees all resources associated
91 sqlsrv_free_stmt($stmt);
92
93 // Save the last PK on pension prod
94 $stmt = $this->db->query("SELECT TOP 1 PK FROM $this->pensionProdScriptStepResults Where machineName = '$machineName' order by ProdRegDate desc");
95 $pensionProdPk = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
96
97 // Frees all resources associated
98 sqlsrv_free_stmt($stmt);
99
100 // When PK equal between 2 tables (panel control & pension prod)
101 if ($panelControlPk['PK'] == $pensionProdPk['PK']){
102 // Finished
103 return FINISHED;
104 }else{
105 // PK Stuck - diffrent between tables
106 return PK.' '.$pensionProdPk['PK'].' '.STUCK;
107 }
108 }else{
109 // Pk Running (PK exists on pension prod but not exists on panel control)
110 return PK.' '.$expectedTime['PK'].' '.RUN;
111 }
112 }
113
114 /*************************************************
115 FUNCTION: getRowsFromDbAndCreateMachine
116 PURPOSE: Return rows from table and create a machine
117 GET VALUES: $workspace (Production or Test)
118 RETURN VALUES:
119 *************************************************/
120 public function getRowsFromDbAndCreateMachine($workspace)
121 {
122 // Create cookie object
123 $cookie = new Cookie();
124 // Return cookie if exists
125 $cookie = $cookie->selectCookiesIfExists();
126
127 // Return rows from panel control by workspace and cookie if exists (each row is one machine)
128 $stmt = $this->db->query("SELECT * from $this->panelControlVmControl Where Environment = '$workspace' $cookie");
129 $buildMachines = 'createMachine';
130
131 while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
132 {
133 $machineName = $row['MachineName'];
134 $environment = $row['Environment'];
135 $status = $row['Status'];
136 $allotment = $row['Allotment'];
137 $logedInUser = $row['LogedInUser'];
138 $workBooks = $row['WorkBooks'];
139 $pkRanges = $row['PkRanges'];
140 $currentPk = $this->getCurrentPk($machineName,$status);
141 $projectName = $row['ProjectName'];
142 $scriptsNo = $row['ScriptsNo'];
143 $loginDateTime = $row['LoginDateTime']->format('H:i d/m/y');
144 $logoutDateTime = $row['LogoutDateTime']->format('H:i d/m/y');
145
146 if ($logedInUser == ""){
147 $logedInUser = NOT_EXISTS;
148 }
149
150 // Create machine object
151 $machine = new machine($machineName, $environment, $status, $allotment, $logedInUser, $workBooks, $pkRanges, $projectName, $scriptsNo, $currentPk, $loginDateTime, $logoutDateTime);
152 $machine->createMachine();
153 }
154
155 // Frees all resources associated
156 sqlsrv_free_stmt($stmt);
157
158 // Close db connection
159 $this->db->close();
160 }
161
162
163 /*************************************************
164 FUNCTION: getRowsFromDbAndCreateMonitor
165 PURPOSE: Return rows from table and create a monitor
166 GET VALUES:
167 RETURN VALUES:
168 *************************************************/
169 public function getRowsFromDbAndCreateMonitor()
170 {
171 $stmt = $this->db->query("SELECT * from $this->panelControlVmControlMonitor");
172 while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
173 {
174 $machineName = $row['MachineName'];
175 $ip = $row['IP'];
176 $logedInUser = $this->getLoginOrLogoutDateTime($machineName,"");
177 $cpu = $row['Cpu'];
178 $ram = $row['Ram'];
179 $cDriveSpace = $row['CdriveSpace'];
180 $cDriveUsage = $row['CdriveUsage'];
181 $dDriveSpace = $row['DdriveSpace'];
182 $dDriveUsage = $row['DdriveUsage'];
183 $cDriveCapacity = $row['CdriveCapacity'];
184 $dDriveCapacity = $row['DdriveCapacity'];
185 $ramCapacity = $row['RamCapacity'];
186 $processorName = $row['ProcessorName'];
187 $upTime = $row['UpTime'];
188 $loginDateTime = $this->getLoginOrLogoutDateTime($machineName,"login");
189 $logoutDateTime = $this->getLoginOrLogoutDateTime($machineName,"logout");
190
191 // Create machine object
192 $monitor = new monitor($machineName, $ip, $logedInUser, $cpu, $ram, $cDriveUsage, $cDriveSpace, $dDriveUsage, $dDriveSpace, $cDriveCapacity, $dDriveCapacity, $ramCapacity, $processorName, $upTime, $loginDateTime, $logoutDateTime);
193 $monitor->createMonitor();
194 }
195
196 // Frees all resources associated
197 sqlsrv_free_stmt($stmt);
198
199 // Close db connection
200 $this->db->close();
201
202 }
203
204
205 /*************************************************
206 FUNCTION: getLoginOrLogoutDateTime
207 PURPOSE: Return login or logout date & time for machine
208 GET VALUES: $machineName, $choiceLoginOrLogout
209 RETURN VALUES: $loginDateTime or $logoutDateTime
210 *************************************************/
211 public function getLoginOrLogoutDateTime($machineName, $choiceLoginOrLogout)
212 {
213 $stmt = $this->db->query("SELECT * from $this->panelControlVmControl Where MachineName = '$machineName'");
214 $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH);
215
216 if ( $choiceLoginOrLogout == "login" ){
217 $loginDateTime = $row['LoginDateTime']->format('H:i d/m/y');
218 return $loginDateTime;
219 } elseif ( $choiceLoginOrLogout == "logout"){
220 $logoutDateTime = $row['LogoutDateTime']->format('H:i d/m/y');
221 return $logoutDateTime;
222 } else{
223 $logedInUser = $row['LogedInUser'];
224 return $logedInUser;
225 }
226 }
227
228 /*************************************************
229 FUNCTION: returnScriptNameOnHeb
230 PURPOSE: Comparison of the columns in the table,
231 scriptEngName vs scriptHebName
232 GET VALUES: $scriptNameEng
233 RETURN VALUES: $scriptNameHeb
234 *************************************************/
235 public function returnScriptNameOnHeb($scriptNameEng)
236 {
237 // Set variables
238 $scriptNameHeb = '';
239
240 // SQL Query
241 $stmt = $this->db->query("SELECT * FROM $this->panelControlScripts WHERE ScriptName = '$scriptNameEng'");
242 while($rowScriptsTable = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
243 $scriptNameHeb = $rowScriptsTable["ScriptNameHeb"];
244 }
245
246 // Frees all resources associated
247 sqlsrv_free_stmt($stmt);
248
249 // Return script hebrew name
250 return $scriptNameHeb;
251 }
252}
253
254?>