linux系统和windows系统检测磁盘大小报警

1.应用场景

需要往磁盘中存储图片和或者数据,需要检测磁盘大小,当小于5GB的时候提示用户,并停止存储图片和数据,避免磁盘塞满,无法启动系统或者操作电脑;

2.实现方法

用一个定时器去定时查询磁盘空间大小,linux系统,采用QProess执行命令,然后解析命令返回值,获取剩余的磁盘空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
void FaceMatch::SlotCheckDriverMemory()
{
    ///磁盘名
    QString strpicPath = CConfig::instance()->GetResaultPath();
#ifdef OS_WINDOWS
    QString iDriver = strpicPath.left(3);
    LPCWSTR strDriver = (LPCWSTR)iDriver.utf16();
    ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;
    ///调用函数获取磁盘参数(单位为字节Byte)
    GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace);
    quint64 freeDB = totalFreeDiskSpace.QuadPart / (1024 * 1024 * 1024);
    m_VideoAnalysisTaskList->SlotSetStoreSpace(freeDB>=5);
    if (freeDB < 5)//space less than 5GB
    {
        if (m_bigThanFiveGB == true)
        {
            m_bigThanFiveGB = false;
            SlotError(-1, strpicPath + "磁盘空间不足5G,请在SystemConfig.xml配置文件切换磁盘,或者清除数据。");
        }
        else
        {
            m_bigThanFiveGB = false;
        }
 
    }
    else//space big than 5GB
    {
        if (m_bigThanFiveGB == false)//space
        {
            m_bigThanFiveGB = true;
            m_VideoAnalysisTaskList->StartNextWaitTask("struct");
            m_VideoAnalysisTaskList->StartNextWaitTask("gait");
            SlotError(0, strpicPath + QString("磁盘空间剩余%1,继续分析").arg(freeDB));
        }
    }
     
#else
    //获取保存的路径的
    QProcess process;
    process.start("df -h " + strpicPath);
    if (process.waitForFinished())
    {
        QString bytes = process.readAll();
        QString strLeftSize=bytes.split("\n").at(1);
        QStringList list=strLeftSize.split(" ");
        for(int i=0;i<list.size();)
        {
            if(list[i]==" "||list[i]=="")
            {
                list.removeAt(i);
            }
            else
            {
                i++;
            }
        }
        if(list.size()<4)
        {
             LOG_ERROR("Get store info error:%s ",bytes.toStdString().c_str());
             return;
        }
       strLeftSize=list[3];
       strLeftSize= strLeftSize.remove("G");
       int freeDB =strLeftSize.toInt();
        m_VideoAnalysisTaskList->SlotSetStoreSpace(freeDB >=5);
       if(freeDB<5)//space less than 5GB
       {
           if(m_bigThanFiveGB==true)
           {
               m_bigThanFiveGB=false;
              SlotError (-1, CConfig::instance()->GetResaultPath()+ "磁盘空间不足5G,请在SystemConfig.xml配置文件切换磁盘,或者清除数据。");
           }
           else
           {
                m_bigThanFiveGB=false;
           }
 
       }
       else//space big than 5GB
       {
           if(m_bigThanFiveGB==false)//space
           {
               m_bigThanFiveGB=true;
               m_VideoAnalysisTaskList->StartNextWaitTask("struct");
               m_VideoAnalysisTaskList->StartNextWaitTask("gait");
                SlotError (0, CConfig::instance()->GetResaultPath() + QString("磁盘空间剩余%1,继续分析").arg(freeDB));
           }
       }
    }
#endif
     
 
}

  

posted @   一字千金  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版
历史上的今天:
2021-10-12 centos防火墙操作firewalld and systemctl 打开linux端口的shell脚本
2021-10-12 Linux快速开发之makefile编译、以服务形式运行、抓包、调试、分析dump
2021-10-12 linux docker 操作命令
点击右上角即可分享
微信分享提示