Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…watcher into lv-zheng-diskspace
  • Loading branch information
htfy96 committed May 16, 2016
2 parents ee15e5d + 884e235 commit aafa3e0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ g++ -std=c++14 -O2 watcher.cpp -o watcher -pthread

## Usage
```
./watcher nginx_access_log_path sleep_second output_json_path
./watcher nginx_access_log_path sleep_second serve_content_root output_json_path
```

Then copy `status.html`, `updatedata.js`, `status.css`, `psd3.min.css`, `psd3.min.js` to the same directory as the json's.
Expand Down
6 changes: 6 additions & 0 deletions status.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ <h1>Server Status</h1>
<div id="memChart" width="800px" height="300px"></div>
</div>
</div>
<div id="diskspace">
Disk space:<br>
<span id="diskspaceused" style="font-family: monospace"></span> GiB used.<br>
<span id="diskspaceavail" style="font-family: monospace"></span> GiB available.
</div>

<div id="hotdir">
<div id="hotdir-content">
</div>
Expand Down
2 changes: 2 additions & 0 deletions updatedata.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
});
memChart.series[1].setData(data.mem.realUsed);

$("#diskspaceused").text((data.diskSpace.used / 1073741824).toFixed(2));
$("#diskspaceavail").text((data.diskSpace.available / 1073741824).toFixed(2));
$("#hotdir-content").html("");
var config = {
containerId : "hotdir-content",
Expand Down
54 changes: 51 additions & 3 deletions watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cctype>
#include <algorithm>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <cstdio>
Expand Down Expand Up @@ -121,6 +122,37 @@ namespace Infos
}
};

struct DiskSpaceInfo
{
// sizes are in bytes. Caution that total != used + avail
std::size_t total, used, avail;
};

class DiskSpaceGetter
{
private:
std::string _path;

public:
explicit DiskSpaceGetter(const std::string &path):
_path(path)
{
}

DiskSpaceInfo getDiskSpace()
{
struct statvfs stat; // c struct in posix
DiskSpaceInfo result;
int statres = statvfs(_path.c_str(), &stat);
if (statres == -1)
return {};
result.total = stat.f_frsize * stat.f_blocks;
result.used = result.total - stat.f_bsize * stat.f_bfree;
result.avail = stat.f_bsize * stat.f_bavail;
return result;
}
};

class NginxLogInfo
{
private:
Expand Down Expand Up @@ -223,6 +255,8 @@ class Status

std::map<std::string, long long> nginxDirInfo;

Infos::DiskSpaceInfo diskSpace;

void updateDirInfoFromItem(const NginxLogEntry &e)
{
if (e.path[0]=='/' && (e.status == 200 || e.status == 304))
Expand Down Expand Up @@ -257,6 +291,11 @@ class Status
}
}

void updateDiskSpace(const Infos::DiskSpaceInfo& ds)
{
diskSpace = ds;
}

std::string stat()
{
std::stringstream ss;
Expand Down Expand Up @@ -353,6 +392,13 @@ void printJSON(std::ofstream& of, const Status& sta)
}
}
}

j["diskSpace"] = {
{ "total", sta.diskSpace.total },
{ "used", sta.diskSpace.used },
{ "available", sta.diskSpace.avail },
};

of << j.dump(4);
}

Expand All @@ -363,11 +409,11 @@ inline std::string currentTime()

int main(int argc, char* argv[])
{
if (argc != 4)
if (argc != 5)
{
cout << R"USAGE(
USAGE:
watcher nginx_log_path sleep_second output_json_path
watcher nginx_log_path sleep_second serve_content_root output_json_path
This is a free software. [email protected])USAGE" << endl;
return 0;
Expand All @@ -376,6 +422,7 @@ int main(int argc, char* argv[])
Infos::CPUInfoGetter cg;
Infos::NginxLogInfo nl(argv[1]);
NginxLogEntryParser np;
Infos::DiskSpaceGetter dg(argv[3]);
Status s;
for (long long cnt = 0;;++cnt)
{
Expand All @@ -386,7 +433,8 @@ int main(int argc, char* argv[])
auto newEntrys = nl.getIncrLines();
for (const auto & item :newEntrys)
s.checkAndAddNginxInfos(np.parse(item));
std::ofstream of(argv[3]);
s.updateDiskSpace(dg.getDiskSpace());
std::ofstream of(argv[4]);
printJSON(of, s);
of.flush();
of.close();
Expand Down

0 comments on commit aafa3e0

Please sign in to comment.