磁盘剩余:代码实现

摘要:

1、系统运维中,需要不定时监控和查看服务器的磁盘剩余空间
2、现在自动化运维工具中,有zabbix可以解决,这里不做展开,这里是代码手段的解决方案
3、关键代码有:
(1)获得磁盘剩余值
(2)磁盘空间大小KB到GB的转换
(3)安全鉴权操作
 

原文链接:
http://www.lookdaima.com/page/docItemDetail.html?id=48e4cd1b-71a4-42f0-b9e5-fd584efffa2b

界面效果:

关键代码:

磁盘空间大小显示的转换

#region FileSizeGetText|文件大小转成MB/GB等内容显示(如:123MB)

/// <summary>
/// 获得文件大小的标签提示 - FileSizeGetLengthName
/// </summary>
/// <param name="fileRate">文件倍数</param>
/// <returns></returns>
protected string FileSizeGetLengthName(int fileRate)
{
    switch (fileRate)
    {
        case 0:
            return "";
        case 1:
            return "KB";
        case 2:
            return "MB";
        case 3:
            return "GB";
        case 4:
            return "TB";
        default:
            return "TB";
    }
}

/// <summary>
/// 文件大小获得提示文字 - FileSizeGetText
/// </summary>
/// <param name="fileLength">文件大小|如:1024012</param>
/// <returns></returns>
protected string FileSizeGetText(long fileLength)
{

    double dLen = (double)fileLength;

    int fileRate = 0;

    while (fileRate < 5)
    {
        if (dLen < 1024)
            break;

        ++fileRate;
        dLen = dLen / 1024;
    }

    dLen = Math.Round(dLen, 2);

    return dLen.ToString() + FileSizeGetLengthName(fileRate);
}

#endregion FileSizeGetText|文件大小转成MB/GB等内容显示(如:123MB)

 

获得磁盘和磁盘信息

System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
    tbSb.AppendLine("<tr>");
    strTmp = IoDrawDriveInfoTdHtml(true, drive.Name, "center");
    tbSb.AppendLine(strTmp);

    strTmp = IoDrawDriveInfoTdHtml(true, drive.VolumeLabel, "center");
    tbSb.AppendLine(strTmp);

    strTmp = FileSizeGetText(drive.TotalFreeSpace);
    strTmp = IoDrawDriveInfoTdHtml(true, strTmp, "center");
    tbSb.AppendLine(strTmp);

    strTmp = FileSizeGetText(drive.TotalSize);
    strTmp = IoDrawDriveInfoTdHtml(true, strTmp, "center");
    tbSb.AppendLine(strTmp);

    tbSb.AppendLine("</tr>");
}

 


判断是否访问的权限

#region 判断是否访问的权限

/// <summary>
/// 判断是否访问的权限
/// </summary>
/// <param name="isRedirect">权限校验失败,是否重定向到指定提示页面</param>
/// <returns></returns>
protected override bool CheckPower(bool isRedirect)
{
    //if (Request.IsLocal)
    //    return true;

    WebUtil wu = WebUtil.GetWebUtilInstance();

    bool adminFlag = wu.UserIsAdmin(null);

    if (adminFlag)
        return true;

    string userName = Request.QueryString["UserName"];
    string userPwd = Request.QueryString["UserPwd"];
    string sign = Request.QueryString["Sign"];

    eKing.EkUtil.Helpers.eKingEkUtilHelper eku = eKing.EkUtil.Helpers.eKingEkUtilHelper.GetInstance();

    string md5 = eku.GetGB2312MD5(userName + userPwd + MD5_KEY);

    if (sign != md5)
    {
        if (isRedirect)
        {
            Response.Redirect(strPhyPath + "/userlogin.aspx");
        }

        return false;
    }

    eKing.BmLib.Business.User.UTB_BM_USER_ITEM bll = eKing.BmLib.Business.User.UTB_BM_USER_ITEM.GetInstance();
    eKing.BmLib.Model.User.UTB_BM_USER_ITEM model = bll.GetModelByUserName(userName, null);

    if (model == null)
    {
        if (isRedirect)
        {
            Response.Redirect(strPhyPath + "/userlogin.aspx");
        }

        return false;

    }

    string md5Pwd = eku.GetGB2312MD5(model.UserName + model.UserPwd + MD5_KEY);

    if (md5Pwd != userPwd)
    {
        if (isRedirect)
        {
            Response.Redirect(strPhyPath + "/userlogin.aspx");
        }

        return false;
    }

    return true;
}

#endregion 判断是否访问的权限

 

完整代码
aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="WebForms_WebPages_Blanks_Common_SysAdms_SysInfos_Drives_Default"
    ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
        <%=WebPageTitle %></title>
    <link href="<%=strPhyPath %>/css/PM.css?v=20170909" rel="stylesheet" media="all" />
    <script language="javascript" type="text/javascript" src="<%=strPhyPath %>/js/My97DatePicker/WdatePicker.js"></script>
    <script language="javascript" type="text/javascript" src="<%=strPhyPath %>/JS/PM.js"></script>
</head>
<body style="padding: 5px;">
    <form id="form1" runat="server">
        <asp:Literal ID="lt_Html" runat="server"></asp:Literal>
    </form>
</body>
</html>

 


aspx.cs

using System;
using System.Reflection;
using System.Web.UI.WebControls;
using SlowX.DAL.Helpers;
using SlowX.Core.ICoreClasses;
using eKing.EkWeb.Classes.PageParams;
using eKing.EkUtil.Helpers;
using eKing.EkWebUtil.Helpers;
using SlowX.Core.Classes.ShowDatas;
using System.Text;

public partial class WebForms_WebPages_Blanks_Common_SysAdms_SysInfos_Drives_Default 
    :
    PageBase
{
    private const string MD5_KEY = "******";

    #region FileSizeGetText|文件大小转成MB/GB等内容显示(如:123MB)

    /// <summary>
    /// 获得文件大小的标签提示 - FileSizeGetLengthName
    /// </summary>
    /// <param name="fileRate">文件倍数</param>
    /// <returns></returns>
    protected string FileSizeGetLengthName(int fileRate)
    {
        switch (fileRate)
        {
            case 0:
                return "";
            case 1:
                return "KB";
            case 2:
                return "MB";
            case 3:
                return "GB";
            case 4:
                return "TB";
            default:
                return "TB";
        }
    }

    /// <summary>
    /// 文件大小获得提示文字 - FileSizeGetText
    /// </summary>
    /// <param name="fileLength">文件大小|如:1024012</param>
    /// <returns></returns>
    protected string FileSizeGetText(long fileLength)
    {

        double dLen = (double)fileLength;

        int fileRate = 0;

        while (fileRate < 5)
        {
            if (dLen < 1024)
                break;

            ++fileRate;
            dLen = dLen / 1024;
        }

        dLen = Math.Round(dLen, 2);

        return dLen.ToString() + FileSizeGetLengthName(fileRate);
    }

    #endregion FileSizeGetText|文件大小转成MB/GB等内容显示(如:123MB)



    /// <summary>
    /// 绘制td或th
    /// </summary>
    /// <param name="tdFlag"></param>
    /// <param name="cellText"></param>
    /// <param name="align"></param>
    /// <returns></returns>
    protected string IoDrawDriveInfoTdHtml(bool tdFlag, string cellText, string align)
    {
        if (align == null || align.Length == 0)
        {
            align = "left";
        }

        if (tdFlag)
        {
            return @"<td align=""" + align + @""" height=""30px"">" + cellText + "</td>";
        }
        else
        {
            return @"<th align=""" + align + @""" height=""30px"">" + cellText + "</th>";
        }
    }

    /// <summary>
    /// 绘制磁盘空间大小的HTML
    /// </summary>
    /// <returns></returns>
    public string IoDrawDriveInfoTableHtml()
    {

        string strTmp = null;
        StringBuilder tbSb = new StringBuilder();

        tbSb.AppendLine("<table class=\"table-oneline\" style=\"width:99%;\">");
        tbSb.AppendLine("<tbody>");

        tbSb.AppendLine("<tr>");
        strTmp = IoDrawDriveInfoTdHtml(false, "磁盘", "center");
        tbSb.AppendLine(strTmp);

        strTmp = IoDrawDriveInfoTdHtml(false, "标签", "center");
        tbSb.AppendLine(strTmp);

        strTmp = IoDrawDriveInfoTdHtml(false, "可用空间", "center");
        tbSb.AppendLine(strTmp);

        strTmp = IoDrawDriveInfoTdHtml(false, "总空间", "center");
        tbSb.AppendLine(strTmp);

        tbSb.AppendLine("</tr>");

        System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
        foreach (System.IO.DriveInfo drive in drives)
        {
            tbSb.AppendLine("<tr>");
            strTmp = IoDrawDriveInfoTdHtml(true, drive.Name, "center");
            tbSb.AppendLine(strTmp);

            strTmp = IoDrawDriveInfoTdHtml(true, drive.VolumeLabel, "center");
            tbSb.AppendLine(strTmp);

            strTmp = FileSizeGetText(drive.TotalFreeSpace);
            strTmp = IoDrawDriveInfoTdHtml(true, strTmp, "center");
            tbSb.AppendLine(strTmp);

            strTmp = FileSizeGetText(drive.TotalSize);
            strTmp = IoDrawDriveInfoTdHtml(true, strTmp, "center");
            tbSb.AppendLine(strTmp);

            tbSb.AppendLine("</tr>");
        }

        tbSb.AppendLine("</tbody>");
        tbSb.AppendLine("</table>");

        return @"
<style type=""text/css"">
    .table-oneline
    {
        -moz-border-bottom-colors: none;
        -moz-border-left-colors: none;
        -moz-border-right-colors: none;
        -moz-border-top-colors: none;
        border-collapse: collapse;
        border-color: #ccc;
        border-image: none;
        border-style: solid;
        border-width: 1px 0 0 1px;
        margin-top: 5px;
    }
    .table-oneline th, .table-oneline td
    {
        -moz-border-bottom-colors: none;
        -moz-border-left-colors: none;
        -moz-border-right-colors: none;
        -moz-border-top-colors: none;
        border-color: #ccc;
        border-image: none;
        border-style: solid;
        border-width: 0 1px 1px 0;
        padding: 6px;
    }
</style>
" + tbSb.ToString();
    }


    /// <summary>
    /// 判断是否登录的权限
    /// </summary>
    /// <param name="isRedirect">权限校验失败,是否重定向到指定提示页面</param>
    /// <returns></returns>
    protected override bool CheckPower(bool isRedirect)
    {
        //if (Request.IsLocal)
        //    return true;

        WebUtil wu = WebUtil.GetWebUtilInstance();

        bool adminFlag = wu.UserIsAdmin(null);

        if (adminFlag)
            return true;

        string userName = Request.QueryString["UserName"];
        string userPwd = Request.QueryString["UserPwd"];
        string sign = Request.QueryString["Sign"];

        eKing.EkUtil.Helpers.eKingEkUtilHelper eku = eKing.EkUtil.Helpers.eKingEkUtilHelper.GetInstance();

        string md5 = eku.GetGB2312MD5(userName + userPwd + MD5_KEY);

        if (sign != md5)
        {
            if (isRedirect)
            {
                Response.Redirect(strPhyPath + "/userlogin.aspx");
            }

            return false;
        }

        eKing.BmLib.Business.User.UTB_BM_USER_ITEM bll = eKing.BmLib.Business.User.UTB_BM_USER_ITEM.GetInstance();
        eKing.BmLib.Model.User.UTB_BM_USER_ITEM model = bll.GetModelByUserName(userName, null);

        if (model == null)
        {
            if (isRedirect)
            {
                Response.Redirect(strPhyPath + "/userlogin.aspx");
            }

            return false;

        }

        string md5Pwd = eku.GetGB2312MD5(model.UserName + model.UserPwd + MD5_KEY);

        if (md5Pwd != userPwd)
        {
            if (isRedirect)
            {
                Response.Redirect(strPhyPath + "/userlogin.aspx");
            }

            return false;
        }

        return true;
    }


    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPower(true))
            return;

        if (!this.IsPostBack)
        {
            string tbHtml = IoDrawDriveInfoTableHtml();
            lt_Html.Text = tbHtml;
        }

        CurPageTitle = "查看看服务器磁盘空间剩余";
        // DebugOper();
    }


}

 

 

posted @ 2021-02-04 20:58  看代码`lookdaima.com  阅读(184)  评论(0编辑  收藏  举报