PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/scalr_newui/app/src/Scalr/UI/Controller/Logs.php

https://github.com/kennethjiang/Wolke
PHP | 330 lines | 281 code | 49 blank | 0 comment | 21 complexity | 0a022779466a123a648b61a39b9f2c2c MD5 | raw file
  1. <?php
  2. class Scalr_UI_Controller_Logs extends Scalr_UI_Controller
  3. {
  4. public function hasAccess()
  5. {
  6. return $this->session->getAuthToken()->hasAccess(Scalr_AuthToken::ACCOUNT_USER);
  7. }
  8. public function systemAction()
  9. {
  10. $farmsController = self::loadController('Farms');
  11. if (is_null($farmsController))
  12. throw new Exception('Controller Farms not created');
  13. else
  14. $farms = $farmsController->getList();
  15. $farms[0] = '所有云平台';
  16. $this->response->setJsonResponse(array(
  17. 'success' => true,
  18. 'module' => $this->response->template->fetchJs('logs/system.js'),
  19. 'moduleParams' => array(
  20. 'farms' => $farms,
  21. 'params' => array(
  22. 'severity[1]' => 0,
  23. 'severity[2]' => 1,
  24. 'severity[3]' => 1,
  25. 'severity[4]' => 1,
  26. 'severity[5]' => 1
  27. ),
  28. 'time' => date('r'),
  29. 'timeOffset' => date('Z')
  30. )
  31. ));
  32. }
  33. public function scriptingAction()
  34. {
  35. $farmsController = self::loadController('Farms');
  36. if (is_null($farmsController))
  37. throw new Exception('Controller Farms not created');
  38. else
  39. $farms = $farmsController->getList();
  40. $farms[0] = 'All farms';
  41. $this->response->setJsonResponse(array(
  42. 'success' => true,
  43. 'module' => $this->response->template->fetchJs('logs/scripting.js'),
  44. 'moduleParams' => array(
  45. 'farms' => $farms,
  46. 'time' => date('r'),
  47. 'timeOffset' => date('Z')
  48. )
  49. ));
  50. }
  51. public function apiAction()
  52. {
  53. $this->response->setJsonResponse(array(
  54. 'success' => true,
  55. 'module' => $this->response->template->fetchJs('logs/api.js'),
  56. 'moduleParams' => array(
  57. 'time' => date('r'),
  58. 'timeOffset' => date('Z')
  59. )
  60. ));
  61. }
  62. public function xGetLogsAction()
  63. {
  64. $this->request->defineParams(array(
  65. 'serverId' => array('type' => 'string'),
  66. 'farmId' => array('type' => 'int'),
  67. 'severity' => array('type' => 'array'),
  68. 'query' => array('type' => 'string'),
  69. 'sort' => array('type' => 'string', 'default' => 'id'),
  70. 'dir' => array('type' => 'string', 'default' => 'DESC')
  71. ));
  72. $farms = $this->db->GetAll("SELECT id FROM farms WHERE env_id=?", array($this->session->getEnvironmentId()));
  73. $frms = array();
  74. foreach ($farms as $f)
  75. array_push($frms, $f['id']);
  76. $frms = implode(',', array_values($frms));
  77. if ($frms == '')
  78. $frms = '0';
  79. $authSql = " AND farmid IN ({$frms}) AND farmid > 0";
  80. $sql = "SELECT * from logentries WHERE id > 0 {$authSql}";
  81. if ($this->getParam('serverId'))
  82. {
  83. $serverId = preg_replace("/[^A-Za-z0-9-]+/si", "", $this->getParam('serverId'));
  84. $sql .= " AND serverid = '{$serverId}'";
  85. }
  86. if ($this->getParam('farmId'))
  87. {
  88. $sql .= " AND farmid = '{$this->getParam('farmId')}'";
  89. }
  90. if ($this->getParam('severity'))
  91. {
  92. $severities = array();
  93. foreach ($this->getParam('severity') as $key => $value) {
  94. if ($value == 1)
  95. $severities[] = $key;
  96. }
  97. if (count($severities)) {
  98. $severities = implode(",", $severities);
  99. $sql .= " AND severity IN ($severities)";
  100. } else {
  101. $sql .= " AND 0"; // is it right ?
  102. }
  103. }
  104. $severities = array(1 => "Debug", 2 => "Info", 3 => "Warning", 4 => "Error", 5 => "Fatal");
  105. if ($this->getParam('action') == "download") {
  106. $fileContent = array();
  107. $farmNames = array();
  108. $fileContent[] = "Type;Time;Farm;Caller;Message\r\n";
  109. $response = $this->buildResponseFromSql($sql, array("message", "serverid", "source"), "", true, true);
  110. foreach($response["data"] as &$data) {
  111. $data["time"] = date("M j, Y H:i:s", $data["time"]);
  112. $data["s_severity"] = $severities[$data["severity"]];
  113. if (!$farmNames[$data['farmid']])
  114. $farmNames[$data['farmid']] = $this->db->GetOne("SELECT name FROM farms WHERE id=?", array($data['farmid']));
  115. $data['farm_name'] = $farmNames[$data['farmid']];
  116. $data['message'] = str_replace("<br />","",$data['message']);
  117. $data['message'] = str_replace("\n","",$data['message']);
  118. $fileContent[] = "{$data['s_severity']};{$data['time']};{$data['farm_name']};{$data['source']};{$data['message']}";
  119. }
  120. $this->response->setHeader('Content-Encoding', 'utf-8');
  121. $this->response->setHeader('Content-Type', 'text/csv', true);
  122. $this->response->setHeader('Expires', 'Mon, 10 Jan 1997 08:00:00 GMT');
  123. $this->response->setHeader('Pragma', 'no-cache');
  124. $this->response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
  125. $this->response->setHeader('Cache-Control', 'post-check=0, pre-check=0');
  126. $this->response->setHeader('Content-Disposition', 'attachment; filename=' . "EventLog_" . date("M_j_Y_H:i:s") . ".csv");
  127. $this->response->setResponse(implode("\n", $fileContent));
  128. $this->response->template->enabled = false;
  129. } else {
  130. $farmNames = array();
  131. $response = $this->buildResponseFromSql($sql, array("message", "serverid", "source"));
  132. foreach ($response["data"] as &$row) {
  133. $row["time"] = date("M j, Y H:i:s", $row["time"]);
  134. $row["servername"] = $row["serverid"];
  135. $row["s_severity"] = $severities[$row["severity"]];
  136. $row["severity"] = (int)$row["severity"];
  137. if (!$farmNames[$row['farmid']])
  138. $farmNames[$row['farmid']] = $this->db->GetOne("SELECT name FROM farms WHERE id=?", array($row['farmid']));
  139. $row['farm_name'] = $farmNames[$row['farmid']];
  140. $row['message'] = nl2br(htmlspecialchars($row['message']));
  141. }
  142. $this->response->setJsonResponse($response);
  143. }
  144. }
  145. public function xGetScriptingLogsAction()
  146. {
  147. $this->request->defineParams(array(
  148. 'farmId' => array('type' => 'int'),
  149. 'query' => array('type' => 'string'),
  150. 'sort' => array('type' => 'string', 'default' => 'id'),
  151. 'dir' => array('type' => 'string', 'default' => 'DESC')
  152. ));
  153. $farms = array();
  154. foreach ($this->db->getAll('SELECT id FROM farms WHERE clientid = ?', array($this->session->getClientId())) as $value)
  155. $farms[] = $value['id'];
  156. $sql = "SELECT id, farmid, event, server_id, dtadded, SUBSTRING(message, 1, 70) AS message FROM scripting_log ";
  157. if ($this->getParam('farmId'))
  158. $sql .= "WHERE farmid = '{$this->getParam('farmId')}'";
  159. else
  160. $sql .= count($farms) ? "WHERE farmid IN (" . implode(',', $farms) . ")" : "WHERE 0";
  161. $response = $this->buildResponseFromSql($sql, array("message", "server_id"));
  162. foreach ($response["data"] as &$row) {
  163. $row["farm_name"] = $this->db->GetOne("SELECT name FROM farms WHERE id=?", array($row['farmid']));
  164. $row["dtadded"] = date("M j, Y H:i:s", strtotime($row["dtadded"]." ".SCALR_SERVER_TZ));
  165. $row['message'] = htmlspecialchars($row['message']);
  166. }
  167. $this->response->setJsonResponse($response);
  168. }
  169. public function xGetApiLogsAction()
  170. {
  171. $this->request->defineParams(array(
  172. 'sort' => array('type' => 'string', 'default' => 'id'),
  173. 'dir' => array('type' => 'string', 'default' => 'DESC')
  174. ));
  175. $sql = "SELECT * from api_log WHERE id > 0 AND clientid='{$this->session->getClientId()}'";
  176. $response = $this->buildResponseFromSql($sql, array("transaction_id"));
  177. foreach ($response["data"] as &$row) {
  178. $row["dtadded"] = date("M j, Y H:i:s", $row["dtadded"]);
  179. }
  180. $this->response->setJsonResponse($response);
  181. }
  182. public function scriptingMessageAction()
  183. {
  184. $entry = $this->db->GetRow("SELECT * FROM scripting_log WHERE id = ?", array($this->getParam('eventId')));
  185. if (empty($entry))
  186. throw new Exception ('Unknown event');
  187. $farm = DBFarm::LoadByID($entry['farmid']);
  188. if ($farm->ClientID != $this->session->getClientId())
  189. throw new Exception('Unknown event');
  190. $form = array(
  191. array(
  192. 'xtype' => 'fieldset',
  193. 'title' => 'Message',
  194. 'layout' => 'fit',
  195. 'items' => array(
  196. array(
  197. 'xtype' => 'textarea',
  198. 'readOnly' => true,
  199. 'hideLabel' => true,
  200. 'height' => 400,
  201. 'value' => $entry['message']
  202. )
  203. )
  204. )
  205. );
  206. $this->response->setJsonResponse(array(
  207. 'success' => true,
  208. 'module' => $this->response->template->fetchJs('logs/scriptingmessage.js'),
  209. 'moduleParams' => $form
  210. ));
  211. }
  212. public function apiLogEntryDetailsAction()
  213. {
  214. $entry = $this->db->GetRow("SELECT * FROM api_log WHERE transaction_id = ? AND clientid = ?", array($this->getParam('transactionId'), $this->session->getClientId()));
  215. if (empty($entry))
  216. throw new Exception ('Unknown transaction');
  217. $entry['dtadded'] = date("M j, Y H:i:s", $entry['dtadded']);
  218. $form = array(
  219. array(
  220. 'xtype' => 'fieldset',
  221. 'title' => 'General information',
  222. 'labelWidth' => 120,
  223. 'items' => array(
  224. array(
  225. 'xtype' => 'displayfield',
  226. 'fieldLabel' => 'Transaction ID',
  227. 'value' => $entry['transaction_id']
  228. ),
  229. array(
  230. 'xtype' => 'displayfield',
  231. 'fieldLabel' => 'Action',
  232. 'value' => $entry['action']
  233. ),
  234. array(
  235. 'xtype' => 'displayfield',
  236. 'fieldLabel' => 'IP address',
  237. 'value' => $entry['ipaddress']
  238. ),
  239. array(
  240. 'xtype' => 'displayfield',
  241. 'fieldLabel' => 'Time',
  242. 'value' => $entry['dtadded']
  243. )
  244. )
  245. ),
  246. array(
  247. 'xtype' => 'fieldset',
  248. 'title' => 'Request',
  249. 'layout' => 'fit',
  250. 'items' => array(
  251. array(
  252. 'xtype' => 'textarea',
  253. 'readOnly' => true,
  254. 'hideLabel' => true,
  255. 'height' => 100,
  256. 'value' => $entry['request']
  257. )
  258. )
  259. ),
  260. array(
  261. 'xtype' => 'fieldset',
  262. 'title' => 'Response',
  263. 'layout' => 'fit',
  264. 'items' => array(
  265. array(
  266. 'xtype' => 'textarea',
  267. 'readOnly' => true,
  268. 'hideLabel' => true,
  269. 'height' => 100,
  270. 'value' => $entry['response']
  271. )
  272. )
  273. )
  274. );
  275. $this->response->setJsonResponse(array(
  276. 'success' => true,
  277. 'module' => $this->response->template->fetchJs('logs/apilogentrydetails.js'),
  278. 'moduleParams' => $form
  279. ));
  280. }
  281. }