Homework 2

From Phobos Wiki
Revision as of 12:14, 20 February 2015 by MarkTomm (talk | contribs) (Uus lehekülg: 'Elementary knowledge of GIT, BASH, C++ will be tested. Task may be completed on any Linux distribution (recommended debian/ubuntu) == GIT == * Save work progress in a repositor...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Elementary knowledge of GIT, BASH, C++ will be tested.

Task may be completed on any Linux distribution (recommended debian/ubuntu)

GIT

  • Save work progress in a repository
  • Use branches (help)
    • One branch for implementing bash script and another branch for c++
    • commit often, total number of commits should be 10+
    • when checking out a random revision, it should compile.
    • Branches start from master and must be merged back into master

BASH

Script 1 - Show memory usage

Script has 1 optional argument, path to meminfo file (contains current memory status). If it's not provided use /proc/meminfo

Use standard unix tools like awk, grep, sed to parse the input file.

Example of input file

cat /proc/meminfo 
MemTotal:        7969612 kB
MemFree:         4582768 kB
Buffers:          707368 kB
Cached:          1122984 kB
SwapCached:            0 kB
Active:          1742408 kB
Inactive:        1203472 kB
[ ... ]

output:

./script1.sh /proc/meminfo
RAM: 4582 MB used / 7969 MB total ( 3387 MB free)

Script 2 - compile c++ application

Compiles the C++ application Probably only has 1 useful line, g++ with some linker options.

example:

./script2.sh
Compilation started
Compilation finished

C++

Parse log file for invalid time stamps

Write a program that parses the provided example log file, Meedia:Ex_log.gz and checks for anomalies in time stamps (if the newer log line has an older time stamp). The normal behavior is that the time increases with each line downwards as shown below

2015-02-19 17:33:08.584 [INFO] Gateway.port9.db -  (122) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.583703, received
2015-02-19 17:33:08.585 [INFO] Gateway.port6.db -  (41) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.585024, received
2015-02-19 17:33:08.586 [INFO] Gateway.port6.db -  (42) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.585943, received
2015-02-19 17:33:08.598 [INFO] Gateway.Port15.db -  (40) event: 1 flags: GwTime GI  time: 2015-Feb-19 17:33:08.551947, sent
2015-02-19 17:33:08.599 [INFO] Gateway.Port15.db -  (120) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.528113, sent
2015-02-19 17:33:08.604 [INFO] Gateway.port9.db -  (123) event: 1 flags: GwTime GI  time: 2015-Feb-19 17:33:08.603814, received
2015-02-19 17:33:08.606 [INFO] Gateway.port6.db -  (43) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.586459, received
2015-02-19 17:33:08.607 [INFO] Gateway.port6.db -  (44) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.606645, received
2015-02-19 17:33:08.607 [INFO] Gateway.port6.db -  (45) event: 2 flags: GwTime GI  time: 2015-Feb-19 17:33:08.607344, received

An erroneous log would look like the next short example

2015-02-19 17:33:08.790 [INFO] Gateway.port10.db -  (128) event: 1 time: 2015-Feb-19 17:31:58.520000, received
2015-02-19 17:33:08.792 [INFO] Gateway.WebServer.db -  (128) event: 1 time: 2015-Feb-19 17:31:58.520000, sent
2015-02-19 11:23:08.808 [INFO] Gateway.Port15.db -  (128) event: 1 time: 2015-Feb-19 17:31:58.520000, sent
2015-02-19 11:23:08.812 [INFO] Gateway.port10.db -  (129) event: 1 time: 2015-Feb-19 17:31:58.855000, received
2015-02-19 11:23:08.814 [INFO] Gateway.WebServer.db -  (129) event: 1 time: 2015-Feb-19 17:31:58.855000, sent
2015-02-19 11:23:08.815 [INFO] Gateway.port10.db -  (130) event: 1 time: 2015-Feb-19 17:31:58.176000, received
2015-02-19 11:23:08.817 [INFO] Gateway.WebServer.db -  (130) event: 1 time: 2015-Feb-19 17:31:58.176000, sent
2015-02-19 17:33:08.817 [INFO] Gateway.port10.db -  (131) event: 1 time: 2015-Feb-19 17:31:58.091000, received
2015-02-19 17:33:08.837 [INFO] Gateway.Port15.db -  (129) event: 1 time: 2015-Feb-19 17:31:58.855000, sent

Note that year-month-day could also be 'wrong'.

2015-02-19 17:33:08.895 [INFO] Gateway.port7.db -  (54) event: -0.058075 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.895493, received
2015-02-19 17:33:08.897 [INFO] Gateway.port8.db -  (81) event: 0.771538 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.896834, received
2015-02-19 17:33:08.898 [INFO] Gateway.port8.db -  (82) event: 0.541642 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.897743, received
2012-01-15 17:33:08.898 [INFO] Gateway.port8.db -  (83) event: -0.523254 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.898286, received
2012-01-15 17:33:08.899 [INFO] Gateway.port8.db -  (84) event: -0.142151 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.898931, received
2012-01-15 17:33:08.899 [INFO] Gateway.port8.db -  (85) event: 0.0126041 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.899393, received
2015-02-19 17:33:08.900 [INFO] Gateway.port8.db -  (86) event: 0.0643635 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.899999, received
2015-02-19 17:33:08.900 [INFO] Gateway.port8.db -  (87) event: -0.507019 flags: GwTime GI Normalized  time: 2015-Feb-19 17:33:08.900444, received

Or both...


Program accepts one out of three optional arguments: stats, list or all. Examples

./a.out --stats
./a.out 
./a.out --all
--stats
    Outputs statistical information about time stamp anomalies.

--list
    Outputs lines with erroneous time stamps along with line numbers.

--all
    Outputs lines with erroneous time stamps along with line numbers 
    and statistical information about time stamp anomalies

Examples

A short example log file

2015-02-19 13:28:33.778 [INFO] Gateway.Port15.db -  (35) event: 2 time: 2015-Feb-19 17:32:02.013000, sent
2015-02-19 13:28:33.778 [INFO] Gateway.Port15.db -  (133) event: 2 time: 2015-Feb-19 17:31:52.336000, sent
2015-02-19 13:28:33.788 [INFO] Gateway.WebServer.db -  (36) event: 1 time: 2015-Feb-19 17:32:01.386000, sent
2015-02-19 13:28:33.791 [INFO] Gateway.WebServer.db -  (134) event: 2 time: 2015-Feb-19 17:31:52.030000, sent
2015-02-19 13:28:33.797 [INFO] Gateway.port10.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, received
2015-02-19 13:28:33.795 [INFO] Gateway.port6.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, received
2015-02-19 13:28:33.804 [INFO] Gateway.WebServer.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
2015-02-19 11:28:33.805 [INFO] Gateway.port10.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, received
2015-02-19 11:28:33.806 [INFO] Gateway.WebServer.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (36) event: 1 time: 2015-Feb-19 17:32:01.386000, sent
2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (134) event: 2 time: 2015-Feb-19 17:31:52.030000, sent
2015-02-19 13:28:33.808 [INFO] Gateway.Port15.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
2015-02-19 13:28:33.810 [INFO] Gateway.port6.db -  (38) event: 2 time: 2015-Feb-19 17:32:01.744000, received
2015-02-19 13:28:33.813 [INFO] Gateway.WebServer.db -  (38) event: 2 time: 2015-Feb-19 17:32:01.744000, sent
2015-02-19 13:28:33.814 [INFO] Gateway.WebServer.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (38) event: 2 time: 2015-Feb-19 17:32:01.744000, sent
2015-01-19 13:18:33.820 [INFO] Gateway.Port15.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
2015-02-19 13:28:33.821 [INFO] Gateway.port10.db -  (137) event: 2 time: 2015-Feb-19 17:31:52.637000, received
2015-02-19 13:28:33.823 [INFO] Gateway.WebServer.db -  (137) event: 2 time: 2015-Feb-19 17:31:52.637000, sent
2015-02-19 13:28:33.824 [INFO] Gateway.port6.db -  (39) event: 2 time: 2015-Feb-19 17:32:02.451000, received
2015-02-19 13:28:33.826 [INFO] Gateway.WebServer.db -  (39) event: 2 time: 2015-Feb-19 17:32:02.451000, sent
$ ./a.out --stats
Number of lines:
22
Invalid time stamps at line(s):
8-11
16-18
$ ./a.out --list
Lines with invalid time stamps:
[7] 2015-02-19 13:28:33.804 [INFO] Gateway.WebServer.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
[8] 2015-02-19 11:28:33.805 [INFO] Gateway.port10.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, received
[9] 2015-02-19 11:28:33.806 [INFO] Gateway.WebServer.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
[10] 2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (36) event: 1 time: 2015-Feb-19 17:32:01.386000, sent
[11] 2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (134) event: 2 time: 2015-Feb-19 17:31:52.030000, sent
[12] 2015-02-19 13:28:33.808 [INFO] Gateway.Port15.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
---
[15] 2015-02-19 13:28:33.814 [INFO] Gateway.WebServer.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
[16] 2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
[17] 2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (38) event: 2 time: 2015-Feb-19 17:32:01.744000, sent
[18] 2015-01-19 13:18:33.820 [INFO] Gateway.Port15.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
[19] 2015-02-19 13:28:33.821 [INFO] Gateway.port10.db -  (137) event: 2 time: 2015-Feb-19 17:31:52.637000, received
$ ./a.out --all
Number of lines:
22
Invalid time stamps at line(s):
8-11
16-18

Lines with invalid time stamps:
[7] 2015-02-19 13:28:33.804 [INFO] Gateway.WebServer.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
[8] 2015-02-19 11:28:33.805 [INFO] Gateway.port10.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, received
[9] 2015-02-19 11:28:33.806 [INFO] Gateway.WebServer.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
[10] 2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (36) event: 1 time: 2015-Feb-19 17:32:01.386000, sent
[11] 2015-02-19 11:28:33.807 [INFO] Gateway.Port15.db -  (134) event: 2 time: 2015-Feb-19 17:31:52.030000, sent
[12] 2015-02-19 13:28:33.808 [INFO] Gateway.Port15.db -  (135) event: 2 time: 2015-Feb-19 17:31:52.035000, sent
---
[15] 2015-02-19 13:28:33.814 [INFO] Gateway.WebServer.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
[16] 2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (37) event: 2 time: 2015-Feb-19 17:32:01.157000, sent
[17] 2015-01-19 13:18:33.819 [INFO] Gateway.Port15.db -  (38) event: 2 time: 2015-Feb-19 17:32:01.744000, sent
[18] 2015-01-19 13:18:33.820 [INFO] Gateway.Port15.db -  (136) event: 1 time: 2015-Feb-19 17:31:52.090000, sent
[19] 2015-02-19 13:28:33.821 [INFO] Gateway.port10.db -  (137) event: 2 time: 2015-Feb-19 17:31:52.637000, received

If no arguments are given, output is same as with stats argument.

Note that with list argument one line before and after the invalid time stamp is also output (look closer at the example outputs).

Program is output to 'standard output'.

Use boost libraries: format, date_time, filesystem.


MarkTomm 20. veebruar 2015, kell 14:14 (EET)