Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oop project #28

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# CppND-System-Monitor

Starter code for System Monitor Project in the Object Oriented Programming Course of the [Udacity C++ Nanodegree Program](https://www.udacity.com/course/c-plus-plus-nanodegree--nd213).
System Monitor Project in the Object Oriented Programming Course of the [Udacity C++ Nanodegree Program](https://www.udacity.com/course/c-plus-plus-nanodegree--nd213).

Follow along with the classroom lesson to complete the project!
Follow along with the classroom lessons to complete the project! Important information about where to find the data on linux for the system monitor is [here](project_info.md).

![System Monitor](images/monitor.png)

Expand Down Expand Up @@ -38,4 +38,4 @@ This project uses [Make](https://www.gnu.org/software/make/). The Makefile has f

5. Implement the `System`, `Process`, and `Processor` classes, as well as functions within the `LinuxParser` namespace.

6. Submit!
6. Submit!
Binary file added images/htop-processor-utilization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-kernel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-memory-utilization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-os-release.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-command.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-memory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-uid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-up-time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-process-utilization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-processes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-processor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-running-processes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-system.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-total-processes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-up-time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/system-monitor-username-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions include/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
#include <string>

namespace Format {
std::string ElapsedTime(long times); // TODO: See src/format.cpp
}; // namespace Format
/**
* @brief This converts a long time in seconds to a string time of format HH:MM:SS
*
* @param[in] times: Long measuring seconds
*
* @return String with format HH:MM:SS for the time
*/
std::string ElapsedTime(long times);
};// namespace Format

#endif
#endif
126 changes: 119 additions & 7 deletions include/linux_parser.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef SYSTEM_PARSER_H
#define SYSTEM_PARSER_H
#ifndef LINUX_PARSER_H
#define LINUX_PARSER_H

#include <fstream>
#include <regex>
#include <string>

namespace LinuxParser {
// Paths
const std::string kProcDirectory{"/proc/"};
const std::string kProcDirectory{"/proc"};
const std::string kCmdlineFilename{"/cmdline"};
const std::string kCpuinfoFilename{"/cpuinfo"};
const std::string kStatusFilename{"/status"};
Expand All @@ -19,12 +19,53 @@ const std::string kOSPath{"/etc/os-release"};
const std::string kPasswordPath{"/etc/passwd"};

// System
/**
* @brief This returns the memory utilization of the system in percentage (0-1)
*
* @return float with memory utilization of system in percentage (0-1)
*/
float MemoryUtilization();

/**
* @brief This returns the uptime of the system in seconds
*
* @return long with uptime of system in seconds
*/
long UpTime();

/**
* @brief This returns pids of the system in seconds
*
* @return vector of pids
*/
std::vector<int> Pids();

/**
* @brief Returns the total number of processes
*
* @return int with total number of processes
*/
int TotalProcesses();

/**
* @brief Returns the number of processes running
*
* @return int with number of processes running
*/
int RunningProcesses();

/**
* @brief This returns the operating system
*
* @return string with operating system
*/
std::string OperatingSystem();

/**
* @brief This returns the kernel
*
* @return string with kernel
*/
std::string Kernel();

// CPU
Expand All @@ -40,18 +81,89 @@ enum CPUStates {
kGuest_,
kGuestNice_
};
std::vector<std::string> CpuUtilization();

/**
* @brief This returns the 10 elements from the cpu line of /proc/stat
*
* @return vec of long where each int corresponds to one of the 10 elements of the cpu utilization
*/
std::vector<long> CpuUtilization();

/**
* @brief This returns the total number of Jiffies for the system (Active + Idle)
*
* @return total number of Jiffies for the system (Active + Idle)
*/
long Jiffies();

/**
* @brief This returns the total number of active Jiffies for the system
*
* @return total number of active Jiffies for the system
*/
long ActiveJiffies();
long ActiveJiffies(int pid);

/**
* @brief This returns the total number of idle Jiffies for the system
*
* @return total number of idle Jiffies for the system
*/
long IdleJiffies();

// Processes
/**
* @brief This returns the command string that started the process
*
* @param[in] pid: process id
*
* @return the command string that started the process
*/
std::string Command(int pid);

/**
* @brief This returns the ram used by the process
*
* @param[in] pid: process id
*
* @return the ram used by the process
*/
std::string Ram(int pid);

/**
* @brief This returns the uid associated with a process
*
* @param[in] pid: process id
*
* @return uid string associated with the pid
*/
std::string Uid(int pid);

/**
* @brief This returns the user associated with a process
*
* @param[in] pid: process id
*
* @return user string associated with the pid
*/
std::string User(int pid);
long int UpTime(int pid);

/**
* @brief This returns the total number of active Jiffies for the process
*
* @param[in] pid: process id
*
* @return total number of active Jiffies for the process
*/
long ActiveJiffies(int pid);

/**
* @brief Read and return the uptime of a process in seconds
*
* @param[in] pid: process id
*
* @return long with uptime of the process in seconds
*/
long UpTime(int pid);
}; // namespace LinuxParser

#endif
#endif
2 changes: 1 addition & 1 deletion include/ncurses_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ void DisplayProcesses(std::vector<Process>& processes, WINDOW* window, int n);
std::string ProgressBar(float percent);
}; // namespace NCursesDisplay

#endif
#endif
69 changes: 57 additions & 12 deletions include/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,62 @@ Basic class for Process representation
It contains relevant attributes as shown below
*/
class Process {
public:
int Pid(); // TODO: See src/process.cpp
std::string User(); // TODO: See src/process.cpp
std::string Command(); // TODO: See src/process.cpp
float CpuUtilization(); // TODO: See src/process.cpp
std::string Ram(); // TODO: See src/process.cpp
long int UpTime(); // TODO: See src/process.cpp
bool operator<(Process const& a) const; // TODO: See src/process.cpp

// TODO: Declare any necessary private members
private:
public:
/**
* @brief Process constructor
*
* @param[in] pid: process id
*
* @return Process object
*/
Process(int pid);

/**
* @brief Process id getter
*
* @return Process id
*/
int Pid();

/**
* @brief This returns the user that started the process
*
* @return user that started the process
*/
std::string User();

/**
* @brief This returns the command string associated with pid_ (passthrough to LinuxParser)
*
* @return command string associated with pid_
*/
std::string Command();

/**
* @brief Return this process's CPU utilization (0->1)
*
* @return process's CPU utilization (0->1)
*/
float CpuUtilization();

/**
* @brief This returns the ram used by the process (passthrough to LinuxParser)
*
* @return the ram used by the process
*/
std::string Ram() const;

/**
* @brief Return the age of this process (in seconds) (passthrough to LinuxParser)
*
* @return age of this process (in seconds)
*/
long UpTime();

bool operator<(Process const& a) const; // Return true if the ram of a is less than process ram

private:
int pid_;
};

#endif
#endif
14 changes: 10 additions & 4 deletions include/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#define PROCESSOR_H

class Processor {
public:
float Utilization(); // TODO: See src/processor.cpp
public:
/**
* @brief This returns the processor utilization (0->1)
*
* @return processor utilization (0->1)
*/
float Utilization();

// TODO: Declare any necessary private members
private:
private:
float prev_tot_jiffies_{0};
float prev_act_jiffies_{0};
};

#endif
76 changes: 61 additions & 15 deletions include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,66 @@
#include "processor.h"

class System {
public:
Processor& Cpu(); // TODO: See src/system.cpp
std::vector<Process>& Processes(); // TODO: See src/system.cpp
float MemoryUtilization(); // TODO: See src/system.cpp
long UpTime(); // TODO: See src/system.cpp
int TotalProcesses(); // TODO: See src/system.cpp
int RunningProcesses(); // TODO: See src/system.cpp
std::string Kernel(); // TODO: See src/system.cpp
std::string OperatingSystem(); // TODO: See src/system.cpp

// TODO: Define any necessary private members
private:
Processor cpu_ = {};
std::vector<Process> processes_ = {};
public:
/**
* @brief Getter for cpu_ member
*
* @return Processor object cpu_
*/
Processor& Cpu();

/**
* @brief Builds a vector of Process objects using the process ids
*
* @return Vector of Process objects (one for each process)
*/
std::vector<Process>& Processes();

/**
* @brief Returns memory utilization of the system in % (0-1) via linux_parser MemoryUtilization()
*
* @return float with memory utilization of system in percentage (0-1)
*/
float MemoryUtilization();

/**
* @brief This returns the uptime of the system in secs (a passthrough to linux_parser UpTime())
*
* @return long with uptime of system in seconds
*/
long UpTime();

/**
* @brief Returns the total number of processes (a passthrough to linux_parser TotalProcesses())
*
* @return int with total number of processes
*/
int TotalProcesses();

/**
* @brief Returns the number of processes running (passthrough to linux_parser RunningProcesses())
*
* @return int with number of processes running
*/
int RunningProcesses();

/**
* @brief This returns the kernel (a passthrough to linux_parser Kernel())
*
* @return string with kernel
*/
std::string Kernel();

/**
* @brief This returns the operating system (a passthrough to linux_parser OperatingSystem())
*
* @return string with operating system
*/
std::string OperatingSystem();

private:
Processor cpu_;
std::vector<Process> processes_;
};

#endif
#endif
Loading