From 9d69f22feac731e7a7b545188ff6bb8eb0f04355 Mon Sep 17 00:00:00 2001 From: Diavolo <edoardo.sanguineti222@gmail.com> Date: Mon, 31 May 2021 09:58:24 +0200 Subject: [PATCH] Init --- .gitignore | 340 +++++++++++++++++++++++++++++++++++++++++++++++++++++ common.c | 15 +++ common.h | 16 +++ main.c | 35 ++++++ monitor.c | 129 ++++++++++++++++++++ monitor.h | 7 ++ utils.c | 22 ++++ utils.h | 6 + 8 files changed, 570 insertions(+) create mode 100644 .gitignore create mode 100644 common.c create mode 100644 common.h create mode 100644 main.c create mode 100644 monitor.c create mode 100644 monitor.h create mode 100644 utils.c create mode 100644 utils.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ce6fdd --- /dev/null +++ b/.gitignore @@ -0,0 +1,340 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb \ No newline at end of file diff --git a/common.c b/common.c new file mode 100644 index 0000000..8097d50 --- /dev/null +++ b/common.c @@ -0,0 +1,15 @@ +#include "common.h" + +// Prints the time +void printTimeStamp(FILE *fp) +{ + time_t ltime; + struct tm result; + char stime[32]; + bzero(stime, sizeof(stime)); + + ltime = time(NULL); + localtime_r(<ime, &result); + asctime_r(&result, stime); + fprintf(fp, "\nTimeStamp: %s\n", stime); +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..1209693 --- /dev/null +++ b/common.h @@ -0,0 +1,16 @@ +#ifndef COMMON_H +#define COMMON_H + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <strings.h> +#include <time.h> +#include <unistd.h> + +void printTimeStamp(FILE *fp); + +#define BUFFER_SIZE 4096 + +#endif /* COMMON_H */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..dcc15b6 --- /dev/null +++ b/main.c @@ -0,0 +1,35 @@ +#include "common.h" +#include <signal.h> +#include "monitor.h" + +volatile sig_atomic_t sigint_received = 0; + +void sigint_handler(int a) +{ + sigint_received = 1; +} + +int main(int argc, char *argv[]) +{ + if (argc < 2) + { + printf("Name of the program is missing\n"); + printf("Usage: <name of the program>\n"); + return 2; + } + + struct sigaction act; + act.sa_handler = sigint_handler; + sigaction(SIGINT, &act, NULL); + + setup(argv[1]); + + while (!sigint_received) + { + analyse(); + sleep(10); + } + + printf("Normal shutdown\n"); + return 0; +} diff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..e10523c --- /dev/null +++ b/monitor.c @@ -0,0 +1,129 @@ +#include "common.h" +#include <errno.h> +#include "monitor.h" +#include "utils.h" + +#include <sys/types.h> +#include <signal.h> + +#define MAX_MEMORY 1048576 +#define PROGRAM_NAME 255 +#define MAX_TRACK_SIZE 30 + +FILE *popen(const char *command, const char *mode); +int pclose(FILE *stream); +char *strerror(int errnum); + +void checkStatus(FILE *file, int *currRealMem, int *peakRealMem, + int *currVirtMem, int *peakVirtMem) +{ + + char result[BUFFER_SIZE]; + bzero(result, sizeof(result)); + + if (file == NULL) + { + perror("popen"); + exit(EXIT_FAILURE); + } + + while (fscanf(file, " %1023s", result) == 1) + { + if (strcmp(result, "VmRSS:") == 0) + { + fscanf(file, " %d", currRealMem); + } + + if (strcmp(result, "VmHWM:") == 0) + { + fscanf(file, " %d", peakRealMem); + } + + if (strcmp(result, "VmSize:") == 0) + { + fscanf(file, " %d", currVirtMem); + } + + if (strcmp(result, "VmPeak:") == 0) + { + fscanf(file, " %d", peakVirtMem); + } + } +} + +void isProcessWild(int currRealMem, int peakRealMem, + int currVirtMem, int peakVirtMem, int ID) +{ + // Logs to a file in case something is up + if (currRealMem > MAX_MEMORY) + { + FILE *fp; + fp = fopen("Diavolo.txt", "a"); + printTimeStamp(fp); + fprintf(fp, "WARNING: Exceeded %d KiB of Real Memory for process ID: %d\n", MAX_MEMORY, ID); + fprintf(fp, "Process ID: %d\ncurrRealMem:%d KiB\npeakRealMem:%d KiB\ncurrVirtMem:%d KiB\npeakVirtMem:%d KiB\n", ID, currRealMem, peakRealMem, currVirtMem, peakVirtMem); + fclose(fp); + // Sends a polite request to terminate + int result = kill(ID, SIGTERM); + if (result) + { + printf("Error while terminating process: %d\n", ID); + printf("Error message: %s\n", strerror(errno)); + } + } +} + +static processID[MAX_TRACK_SIZE] = {0}; + +void setup(const char *pid) +{ + char *command = va("pgrep %s", pid); + FILE *file = popen(command, "r"); + + if (file == NULL) + { + perror("popen"); + exit(EXIT_FAILURE); + } + + char result[BUFFER_SIZE]; + bzero(result, sizeof(result)); + int i = 0; + + while (fgets(result, BUFFER_SIZE, file)) + { + printf("Process ID: %s", result); + processID[i] = atoi(result); + i++; + + if (i > MAX_TRACK_SIZE - 1) + { + printf("Too many processes found\n"); + break; + } + } + + pclose(file); +} + +void analyse() +{ + int i; + FILE *status; + int currRealMem = 0, peakRealMem = 0, currVirtMem = 0, peakVirtMem = 0; + + for (i = 0; i < MAX_TRACK_SIZE && processID[i] != 0; i++) + { + char *command = va("cat /proc/%d/status", processID[i]); + status = popen(command, "r"); + if (file == NULL) + { + perror("popen"); + exit(EXIT_FAILURE); + } + + checkStatus(status, &currRealMem, &peakRealMem, &currVirtMem, &peakVirtMem); + isProcessWild(currRealMem, peakRealMem, currVirtMem, peakVirtMem, processID[i]); + pclose(status); + } +} diff --git a/monitor.h b/monitor.h new file mode 100644 index 0000000..a068eb7 --- /dev/null +++ b/monitor.h @@ -0,0 +1,7 @@ +#ifndef MONITOR_H +#define MONITOR_H + +void setup(const char *pid); +void analyse(); + +#endif /* MONITOR_H */ diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..75abd1d --- /dev/null +++ b/utils.c @@ -0,0 +1,22 @@ +#include "common.h" +#include "utils.h" + +#define VA_BUFFER_COUNT 4 +#define VA_BUFFER_SIZE 4096 + +char *va(const char *fmt, ...) +{ + static char g_vaBuffer[VA_BUFFER_COUNT][VA_BUFFER_SIZE]; + static int g_vaNextBufferIndex = 0; + + va_list ap; + char *dest = &g_vaBuffer[g_vaNextBufferIndex][0]; + bzero(dest, VA_BUFFER_SIZE); + g_vaNextBufferIndex = (g_vaNextBufferIndex + 1) % VA_BUFFER_COUNT; + va_start(ap, fmt); + vsnprintf(dest, VA_BUFFER_SIZE, fmt, ap); + va_end(ap); + dest[VA_BUFFER_SIZE - 1] = '\0'; + + return dest; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..6ef62d4 --- /dev/null +++ b/utils.h @@ -0,0 +1,6 @@ +#ifndef UTILS_H +#define UTILS_H + +char *va(const char *fmt, ...); + +#endif /* UTILS_H */