PageRenderTime 68ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/System/Controls/System.ascx.cs

https://github.com/anilbajracharya/Kentico
C# | 626 lines | 440 code | 112 blank | 74 comment | 43 complexity | 61aa6672d5477f4c1d0ce6ddc5b26eaf MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.DirectoryServices;
  8. using CMS.GlobalHelper;
  9. using CMS.CMSHelper;
  10. using CMS.SettingsProvider;
  11. using CMS.URLRewritingEngine;
  12. using CMS.UIControls;
  13. using CMS.EventLog;
  14. using CMS.DataEngine;
  15. using CMS.IO;
  16. using CMS.PortalControls;
  17. using CMS.WebFarmSync;
  18. using CMS.SiteProvider;
  19. using CMS.LicenseProvider;
  20. public partial class CMSModules_System_Controls_System : CMSAdminControl
  21. {
  22. #region "Variables"
  23. private static DateTime mLastRPS = DateTime.MinValue;
  24. private static long mLastPageRequests = 0;
  25. private static long mLastNonPageRequests = 0;
  26. private static long mLastSystemPageRequests = 0;
  27. private static long mLastPageNotFoundRequests = 0;
  28. private static long mLastGetFileRequests = 0;
  29. private static double mRPSPageRequests = -1;
  30. private static double mRPSNonPageRequests = -1;
  31. private static double mRPSSystemPageRequests = -1;
  32. private static double mRPSPageNotFoundRequests = -1;
  33. private static double mRPSGetFileRequests = -1;
  34. #endregion
  35. #region "Properties"
  36. /// <summary>
  37. /// Gets or sets the refresh interval (in seconds) for the page. If set to 0 then no refresh.
  38. /// </summary>
  39. public int RefreshInterval
  40. {
  41. get
  42. {
  43. return (int)(timRefresh.Interval / 1000);
  44. }
  45. set
  46. {
  47. if (value == 0)
  48. {
  49. timRefresh.Enabled = false;
  50. }
  51. else
  52. {
  53. timRefresh.Enabled = true;
  54. timRefresh.Interval = value * 1000;
  55. if (this.drpRefresh.Items.FindByValue(value.ToString()) != null)
  56. {
  57. this.drpRefresh.SelectedValue = value.ToString();
  58. }
  59. }
  60. }
  61. }
  62. #endregion
  63. #region "Page Methods"
  64. protected void Page_Load(object sender, EventArgs e)
  65. {
  66. this.timRefresh.Interval = 1000 * ValidationHelper.GetInteger(this.drpRefresh.SelectedValue, 0);
  67. // Check permissions
  68. RaiseOnCheckPermissions("READ", this);
  69. if (StopProcessing)
  70. {
  71. return;
  72. }
  73. // Get values from counters
  74. long totalSystemRequests = RequestHelper.TotalSystemPageRequests.GetValue(null);
  75. long totalPageRequests = RequestHelper.TotalPageRequests.GetValue(null);
  76. long totalPageNotFoundRequests = RequestHelper.TotalPageNotFoundRequests.GetValue(null);
  77. long totalNonPageRequests = RequestHelper.TotalNonPageRequests.GetValue(null);
  78. long totalGetFileRequests = RequestHelper.TotalGetFileRequests.GetValue(null);
  79. // Reevaluate RPS
  80. if (mLastRPS != DateTime.MinValue)
  81. {
  82. double seconds = DateTime.Now.Subtract(mLastRPS).TotalSeconds;
  83. if ((seconds < 3) && (seconds > 0))
  84. {
  85. mRPSSystemPageRequests = (totalSystemRequests - mLastSystemPageRequests) / seconds;
  86. mRPSPageRequests = (totalPageRequests - mLastPageRequests) / seconds;
  87. mRPSPageNotFoundRequests = (totalPageNotFoundRequests - mLastPageNotFoundRequests) / seconds;
  88. mRPSNonPageRequests = (totalNonPageRequests - mLastNonPageRequests) / seconds;
  89. mRPSGetFileRequests = (totalGetFileRequests - mLastGetFileRequests) / seconds;
  90. }
  91. else
  92. {
  93. mRPSGetFileRequests = -1;
  94. mRPSNonPageRequests = -1;
  95. mRPSPageNotFoundRequests = -1;
  96. mRPSPageRequests = -1;
  97. mRPSSystemPageRequests = -1;
  98. }
  99. }
  100. mLastRPS = DateTime.Now;
  101. // Update last values
  102. mLastGetFileRequests = totalGetFileRequests;
  103. mLastNonPageRequests = totalNonPageRequests;
  104. mLastPageNotFoundRequests = totalPageNotFoundRequests;
  105. mLastPageRequests = totalPageRequests;
  106. mLastSystemPageRequests = totalSystemRequests;
  107. // Do not count this page with async postback
  108. if (RequestHelper.IsAsyncPostback())
  109. {
  110. RequestHelper.TotalSystemPageRequests.Decrement(null);
  111. }
  112. pnlSystemInfo.GroupingText = GetString("Administration-System.SystemInfo");
  113. pnlSystemTime.GroupingText = GetString("Administration-System.SystemTimeInfo");
  114. pnlDatabaseInfo.GroupingText = GetString("Administration-System.DatabaseInfo");
  115. lblServerName.Text = GetString("Administration-System.ServerName");
  116. lblServerVersion.Text = GetString("Administration-System.ServerVersion");
  117. lblDBName.Text = GetString("Administration-System.DatabaseName");
  118. lblDBSize.Text = GetString("Administration-System.DatabaseSize");
  119. lblMachineName.Text = GetString("Administration-System.MachineName");
  120. lblMachineNameValue.Text = HTTPHelper.MachineName;
  121. lblAspAccount.Text = GetString("Administration-System.Account");
  122. lblValueAspAccount.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  123. // Get application pool name
  124. lblPool.Text = GetString("Administration-System.Pool");
  125. lblValuePool.Text = GetString("General.NA");
  126. try
  127. {
  128. lblValuePool.Text = SystemHelper.GetApplicationPoolName();
  129. }
  130. catch
  131. {
  132. }
  133. // Get application trust level
  134. lblTrustLevel.Text = GetString("Administration-System.TrustLevel");
  135. lblValueTrustLevel.Text = GetString("General.NA");
  136. try
  137. {
  138. lblValueTrustLevel.Text = SystemHelper.CurrentTrustLevel.ToString();
  139. }
  140. catch
  141. {
  142. }
  143. lblAspVersion.Text = GetString("Administration-System.Version");
  144. lblValueAspVersion.Text = System.Environment.Version.ToString();
  145. pnlMemory.GroupingText = GetString("Administration-System.MemoryStatistics");
  146. lblAlocatedMemory.Text = GetString("Administration-System.Memory");
  147. lblPeakMemory.Text = GetString("Administration-System.PeakMemory");
  148. lblVirtualMemory.Text = GetString("Administration-System.VirtualMemory");
  149. lblPhysicalMemory.Text = GetString("Administration-System.PhysicalMemory");
  150. lblIP.Text = GetString("Administration-System.IP");
  151. pnlPageViews.GroupingText = GetString("Administration-System.PageViews");
  152. lblPageViewsValues.Text = GetString("Administration-System.PageViewsValues");
  153. lblPages.Text = GetString("Administration-System.Pages");
  154. lblSystemPages.Text = GetString("Administration-System.SystemPages");
  155. lblNonPages.Text = GetString("Administration-System.NonPages");
  156. lblGetFilePages.Text = GetString("Administration-System.GetFilePages");
  157. lblPagesNotFound.Text = GetString("Administration-System.PagesNotFound");
  158. lblPending.Text = GetString("Administration-System.Pending");
  159. pnlCache.GroupingText = GetString("Administration-System.CacheStatistics");
  160. lblCacheExpired.Text = GetString("Administration-System.CacheExpired");
  161. lblCacheRemoved.Text = GetString("Administration-System.CacheRemoved");
  162. lblCacheUnderused.Text = GetString("Administration-System.CacheUnderused");
  163. lblCacheItems.Text = GetString("Administration-System.CacheItems");
  164. lblCacheDependency.Text = GetString("Administration-System.CacheDependency");
  165. pnlGC.GroupingText = GetString("Administration-System.GC");
  166. btnClear.Text = GetString("Administration-System.btnClear");
  167. btnClearCache.Text = GetString("Administration-System.btnClearCache");
  168. btnRestart.Text = GetString("Administration-System.btnRestart");
  169. btnRestartWebfarm.Text = GetString("Administration-System.btnRestartWebfarm");
  170. btnClearCounters.Text = GetString("Administration-System.btnClearCounters");
  171. btnRestartServices.Text = GetString("Administration-System.btnRestartServices");
  172. // Hide button if wefarms are not enabled or disallow restarting webfarms for Windows Azure
  173. if (!WebSyncHelperClass.WebFarmEnabled || AzureHelper.IsRunningOnAzure)
  174. {
  175. this.btnRestartWebfarm.Visible = false;
  176. }
  177. // Hide the web farm restart button if this web farm server is not enabled
  178. if (!RequestHelper.IsPostBack())
  179. {
  180. WebFarmServerInfo webFarmServerObj = WebFarmServerInfoProvider.GetWebFarmServerInfo(WebSyncHelperClass.ServerName);
  181. if ((webFarmServerObj != null) && (!webFarmServerObj.ServerEnabled))
  182. {
  183. this.btnRestartWebfarm.Visible = false;
  184. }
  185. }
  186. // Hide restart service button if services folder doesn't exist
  187. this.btnRestartServices.Visible = SystemHelper.IsFullTrustLevel && WinServiceHelper.ServicesAvailable();
  188. // Hide the performance counters clear button if the health monitoring is not enabled or feature is not available
  189. this.btnClearCounters.Visible = SystemHelper.IsFullTrustLevel && LicenseHelper.CheckFeature(URLHelper.GetCurrentDomain(), FeatureEnum.HealthMonitoring);
  190. LoadData();
  191. if (!RequestHelper.IsPostBack())
  192. {
  193. switch (QueryHelper.GetString("lastaction", String.Empty).ToLower())
  194. {
  195. case "restarted":
  196. lblInfo.Text = GetString("Administration-System.RestartSuccess");
  197. break;
  198. case "webfarmrestarted":
  199. if (ValidationHelper.ValidateHash("WebfarmRestarted", QueryHelper.GetString("restartedhash", String.Empty)))
  200. {
  201. lblInfo.Text = GetString("Administration-System.WebframRestarted");
  202. // Restart other servers - create webfarm task for application restart
  203. WebSyncHelperClass.CreateTask(WebFarmTaskTypeEnum.RestartApplication, "RestartApplication", "", null);
  204. }
  205. else
  206. {
  207. lblInfo.Text = GetString("general.actiondenied");
  208. }
  209. break;
  210. case "countercleared":
  211. lblInfo.Text = GetString("Administration-System.CountersCleared");
  212. break;
  213. case "winservicesrestarted":
  214. lblInfo.Text = GetString("Administration-System.WinServicesRestarted");
  215. break;
  216. }
  217. }
  218. lblRunTime.Text = GetString("Administration.System.RunTime");
  219. lblServerTime.Text = GetString("Administration.System.ServerTime");
  220. // Remove miliseconds from the end of the time string
  221. string timeSpan = (DateTime.Now - CMSAppBase.ApplicationStart).ToString();
  222. int index = timeSpan.LastIndexOf('.');
  223. if (index >= 0)
  224. {
  225. timeSpan = timeSpan.Remove(index);
  226. }
  227. // Display application run time
  228. lblRunTimeValue.Text = timeSpan;
  229. lblServerTimeValue.Text = Convert.ToString(DateTime.Now) + " " + TimeZoneHelper.GetGMTStringOffset(TimeZoneHelper.ServerTimeZone);
  230. lblIPValue.Text = Request.UserHostAddress;
  231. }
  232. /// <summary>
  233. /// Page PreRender event
  234. /// </summary>
  235. protected void Page_PreRender(object sender, EventArgs e)
  236. {
  237. lblInfo.Visible = !String.IsNullOrEmpty(lblInfo.Text);
  238. lblError.Visible = !String.IsNullOrEmpty(lblError.Text);
  239. }
  240. #endregion
  241. #region "Page Events"
  242. /// <summary>
  243. /// Clear unused memory.
  244. /// </summary>
  245. protected void btnClear_Click(object sender, EventArgs e)
  246. {
  247. if (StopProcessing)
  248. {
  249. return;
  250. }
  251. // Collect the memory
  252. GC.Collect();
  253. GC.WaitForPendingFinalizers();
  254. // Log event
  255. EventLogProvider eventLog = new EventLogProvider();
  256. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "CLEARMEM", null, GetString("Administration-System.ClearSuccess"));
  257. lblInfo.Text = GetString("Administration-System.ClearSuccess");
  258. LoadData();
  259. }
  260. /// <summary>
  261. /// Restart application.
  262. /// </summary>
  263. protected void btnRestart_Click(object sender, EventArgs e)
  264. {
  265. if (StopProcessing)
  266. {
  267. return;
  268. }
  269. if (RestartApplication())
  270. {
  271. if (AzureHelper.IsRunningOnAzure)
  272. {
  273. // Log event
  274. EventLogProvider eventLog = new EventLogProvider();
  275. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "ENDAPP", null, GetString("admin.system.restartazure"));
  276. lblInfo.Text = ResHelper.GetString("admin.system.restartazure");
  277. }
  278. else
  279. {
  280. // Log event
  281. EventLogProvider eventLog = new EventLogProvider();
  282. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "ENDAPP", null, GetString("Administration-System.RestartSuccess"));
  283. string url = URLHelper.UpdateParameterInUrl(URLRewriter.CurrentURL, "lastaction", "Restarted");
  284. URLHelper.Redirect(url);
  285. }
  286. }
  287. else
  288. {
  289. lblError.Visible = true;
  290. lblError.Text = GetString("System.Restart.Failed");
  291. }
  292. }
  293. /// <summary>
  294. /// Restart webfarm.
  295. /// </summary>
  296. protected void btnRestartWebfarm_Click(object sender, EventArgs e)
  297. {
  298. if (StopProcessing)
  299. {
  300. return;
  301. }
  302. // Restart current server
  303. RestartApplication();
  304. // Log event
  305. EventLogProvider eventLog = new EventLogProvider();
  306. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "RESTARTWEBFARM", null, GetString("Administration-System.WebframRestarted"));
  307. string url = URLHelper.UpdateParameterInUrl(URLRewriter.CurrentURL, "lastaction", "WebfarmRestarted");
  308. url = URLHelper.UpdateParameterInUrl(url, "restartedhash", ValidationHelper.GetHashString("WebfarmRestarted"));
  309. URLHelper.Redirect(url);
  310. }
  311. /// <summary>
  312. /// Clear counters.
  313. /// </summary>
  314. protected void btnClearCounters_Click(object sender, EventArgs e)
  315. {
  316. if (StopProcessing)
  317. {
  318. return;
  319. }
  320. // Reset values of health monitoring counters
  321. HealthMonitoringManager.ResetCounters();
  322. // Clear application counters
  323. HealthMonitoringLogHelper.ClearApplicationCounters();
  324. // Log event
  325. EventLogProvider eventLog = new EventLogProvider();
  326. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "CLEARCOUNTERS", null, GetString("Administration-System.CountersCleared"));
  327. string url = URLHelper.UpdateParameterInUrl(URLRewriter.CurrentURL, "lastaction", "CounterCleared");
  328. URLHelper.Redirect(url);
  329. }
  330. /// <summary>
  331. /// Restart windows services.
  332. /// </summary>
  333. protected void btnRestartServices_Click(object sender, EventArgs e)
  334. {
  335. if (StopProcessing)
  336. {
  337. return;
  338. }
  339. // Resets values of counters
  340. try
  341. {
  342. WinServiceHelper.RestartService(null);
  343. // Log event
  344. EventLogProvider eventLog = new EventLogProvider();
  345. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "RESTARTWINSERVICES", null, GetString("Administration-System.WinServicesRestarted"));
  346. }
  347. catch (Exception ex)
  348. {
  349. EventLogProvider.LogException("WinServiceHelper", "RestartService", ex);
  350. }
  351. string url = URLHelper.UpdateParameterInUrl(URLRewriter.CurrentURL, "lastaction", "WinServicesRestarted");
  352. URLHelper.Redirect(url);
  353. }
  354. protected void btnClearCache_Click(object sender, EventArgs e)
  355. {
  356. if (StopProcessing)
  357. {
  358. return;
  359. }
  360. // Clear the cache
  361. CacheHelper.ClearCache(null, true);
  362. Functions.ClearHashtables();
  363. // Drop the routes
  364. CMSMvcHandler.DropAllRoutes();
  365. // Collect the memory
  366. GC.Collect();
  367. GC.WaitForPendingFinalizers();
  368. // Log event
  369. EventLogProvider eventLog = new EventLogProvider();
  370. eventLog.LogEvent(EventLogProvider.EVENT_TYPE_INFORMATION, DateTime.Now, "System", "CLEARCACHE", null, GetString("Administration-System.ClearCacheSuccess"));
  371. lblInfo.Text = GetString("Administration-System.ClearCacheSuccess");
  372. LoadData();
  373. }
  374. #endregion
  375. #region "Other Methods"
  376. /// <summary>
  377. /// Loads the data.
  378. /// </summary>
  379. protected void LoadData()
  380. {
  381. lblValueAlocatedMemory.Text = DataHelper.GetSizeString(GC.GetTotalMemory(false));
  382. lblValueVirtualMemory.Text = "N/A";
  383. lblValuePhysicalMemory.Text = "N/A";
  384. lblValuePeakMemory.Text = "N/A";
  385. // Process memory
  386. try
  387. {
  388. lblValueVirtualMemory.Text = DataHelper.GetSizeString(SystemHelper.GetVirtualMemorySize());
  389. lblValuePhysicalMemory.Text = DataHelper.GetSizeString(SystemHelper.GetWorkingSetSize());
  390. lblValuePeakMemory.Text = DataHelper.GetSizeString(SystemHelper.GetPeakWorkingSetSize());
  391. }
  392. catch
  393. {
  394. }
  395. this.lblValuePages.Text = GetViewValues(RequestHelper.TotalPageRequests.GetValue(null), 0, mRPSPageRequests);
  396. this.lblValuePagesNotFound.Text = GetViewValues(RequestHelper.TotalPageNotFoundRequests.GetValue(null), 0, mRPSPageNotFoundRequests);
  397. this.lblValueSystemPages.Text = GetViewValues(RequestHelper.TotalSystemPageRequests.GetValue(null), 0, mRPSSystemPageRequests);
  398. this.lblValueNonPages.Text = GetViewValues(RequestHelper.TotalNonPageRequests.GetValue(null), 0, mRPSNonPageRequests);
  399. this.lblValueGetFilePages.Text = GetViewValues(RequestHelper.TotalGetFileRequests.GetValue(null), 0, mRPSGetFileRequests);
  400. long pending = RequestHelper.PendingRequests.GetValue(null);
  401. if (pending > 1)
  402. {
  403. // Current request does not count as pending at the time of display
  404. pending--;
  405. }
  406. if (pending < 0)
  407. {
  408. pending = 0;
  409. }
  410. this.lblValuePending.Text = pending.ToString();
  411. this.lblCacheItemsValue.Text = Cache.Count.ToString();
  412. this.lblCacheExpiredValue.Text = CacheHelper.Expired.GetValue(null).ToString();
  413. this.lblCacheRemovedValue.Text = CacheHelper.Removed.GetValue(null).ToString();
  414. this.lblCacheUnderusedValue.Text = CacheHelper.Underused.GetValue(null).ToString();
  415. this.lblCacheDependencyValue.Text = CacheHelper.DependencyChanged.GetValue(null).ToString();
  416. // GC collections
  417. try
  418. {
  419. this.plcGC.Controls.Clear();
  420. int generations = GC.MaxGeneration;
  421. for (int i = 0; i <= generations; i++)
  422. {
  423. int count = GC.CollectionCount(i);
  424. string genString = "<tr><td style=\"white-space: nowrap; width: 200px;\">" + GetString("GC.Generation") + " " + i.ToString() + ":</td><td>" + count.ToString() + "</td></tr>";
  425. this.plcGC.Controls.Add(new LiteralControl(genString));
  426. }
  427. }
  428. catch
  429. {
  430. }
  431. lblDBNameValue.Text = TableManager.DatabaseName;
  432. lblServerVersionValue.Text = TableManager.DatabaseServerVersion;
  433. // DB information
  434. if (!RequestHelper.IsPostBack())
  435. {
  436. lblDBSizeValue.Text = TableManager.DatabaseSize;
  437. lblServerNameValue.Text = TableManager.DatabaseServerName;
  438. }
  439. }
  440. /// <summary>
  441. /// Gets the values string for the page views.
  442. /// </summary>
  443. private string GetViewValues(long total, long async, double rps)
  444. {
  445. return total.ToString() /*+ " / " + async.ToString() + " / "*/ + ((rps >= 1) ? " (" + Math.Floor(rps).ToString() + " RPS)" : "");
  446. }
  447. /// <summary>
  448. /// Check user settings if site manager is not explicitly denied for current user.
  449. /// If it is user is redirected to access denied page.
  450. /// </summary>
  451. private void CheckAccessToSiteManager()
  452. {
  453. CurrentUserInfo currentUser = CMSContext.CurrentUser;
  454. if (currentUser != null)
  455. {
  456. if (currentUser.UserSiteManagerDisabled)
  457. {
  458. RedirectToAccessDenied(ResHelper.GetString("accessdeniedtopage.sitemanagerdenied"));
  459. }
  460. }
  461. }
  462. /// <summary>
  463. /// Checks if the user is global administrator (can access the administration (development) page.
  464. /// </summary>
  465. private void CheckGlobalAdministrator()
  466. {
  467. if (!RequestHelper.IsAsyncPostback())
  468. {
  469. CurrentUserInfo currentUser = CMSContext.CurrentUser;
  470. if (currentUser == null)
  471. {
  472. URLHelper.Redirect(mAccessDeniedPage);
  473. }
  474. else if (!currentUser.UserSiteManagerAdmin)
  475. {
  476. URLHelper.Redirect(mAccessDeniedPage);
  477. }
  478. }
  479. }
  480. /// <summary>
  481. /// Tries to restart application and returns if restart was succesfull.
  482. /// </summary>
  483. private bool RestartApplication()
  484. {
  485. // Restart azure instance
  486. if (AzureHelper.IsRunningOnAzure)
  487. {
  488. AzureHelper.RestartAzureInstance();
  489. }
  490. // Restart classic application
  491. else
  492. {
  493. try
  494. {
  495. // Try to restart applicatin by unload app domain
  496. HttpRuntime.UnloadAppDomain();
  497. }
  498. catch
  499. {
  500. try
  501. {
  502. // Try to restart application by changing web.config file
  503. File.SetLastWriteTimeUtc(Request.PhysicalApplicationPath + "\\web.config", DateTime.UtcNow);
  504. }
  505. catch
  506. {
  507. return false;
  508. }
  509. }
  510. }
  511. return true;
  512. }
  513. #endregion
  514. }