tplot 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *~
2
+ log/*
2
3
  *.gem
3
4
  *.rbc
4
5
  .bundle
data/bin/tplot CHANGED
@@ -6,7 +6,14 @@ trap(:INT) do
6
6
  end
7
7
 
8
8
  BASE_DIR = File.expand_path(__FILE__.gsub(%r{/[^/]+\z},'') + "/../")
9
- plugin_name = ARGV.first.nil? ? "list" : ARGV.first
9
+ plugin_name = ARGV.shift
10
+ plugin_name ||= "list"
11
+
12
+ PLUGIN_DIR = "#{BASE_DIR}/plugins"
13
+ USER_PLUGIN_DIR = "#{ENV['HOME']}/.tplot/plugins"
14
+
15
+ require 'fileutils'
16
+ FileUtils.mkdir_p(USER_PLUGIN_DIR)
10
17
 
11
18
  # debug setting
12
19
  require 'logger'
@@ -18,13 +25,15 @@ else
18
25
  end
19
26
 
20
27
  # plugin check
21
- unless File.exists?("#{BASE_DIR}/plugins/#{plugin_name}.rb")
28
+ if(!File.exists?("#{PLUGIN_DIR}/#{plugin_name}.rb") and
29
+ !File.exists?("#{USER_PLUGIN_DIR}/#{plugin_name}.rb"))
22
30
  puts "#{plugin_name} plugin is not found."
23
31
  puts
24
32
  plugin_name = "list"
25
33
  end
26
34
 
27
35
  # load basic module
36
+ require 'optparse'
28
37
  require 'curses'
29
38
  require "#{BASE_DIR}/lib/tplot"
30
39
  require "#{BASE_DIR}/lib/tplot/plugin"
@@ -34,4 +43,4 @@ require "#{BASE_DIR}/lib/tplot/bar_chart"
34
43
  # execute plugin
35
44
  require "#{BASE_DIR}/plugins/#{plugin_name}"
36
45
  plugin_class = Tplot.const_get(plugin_name.capitalize)
37
- plugin_class.new(ARGV.shift).execute
46
+ plugin_class.new.execute
@@ -89,13 +89,16 @@ module Tplot
89
89
 
90
90
  def calc_mapping_value(value)
91
91
  range = (@max_value - @min_value).abs
92
+ return 0 if range == 0
92
93
  ratio = (value - @min_value).abs.to_f / range.to_f
93
94
  (@height * ratio * 2).to_i
94
95
  end
95
96
 
96
97
  def calc_position(value)
97
98
  range = (@max_value - @min_value).abs
99
+ return 0 if range == 0
98
100
  ratio = (value - @min_value).abs.to_f / range.to_f
101
+ LOGGER.debug "#{range}:#{ratio}"
99
102
  (@height * (1 - ratio)).to_i
100
103
  end
101
104
 
data/lib/tplot/plugin.rb CHANGED
@@ -1,8 +1,4 @@
1
1
  class Tplot::Plugin
2
- def initialize(args = [])
3
- @args = args
4
- end
5
-
6
2
  def self.description(description = "")
7
3
  @@descriptions ||= {}
8
4
  @@descriptions[self] = description if !description.nil? and !description.empty?
data/lib/tplot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tplot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/tplot.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "tplot/version"
1
+ require_relative "tplot/version"
2
2
 
3
3
  module Tplot
4
4
  # noop
data/plugins/free.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  class Tplot::Free < Tplot::Plugin
2
2
 
3
- description "plot free commands result"
4
-
5
3
  def execute
6
4
  chart = Tplot::BarChart.new
7
- chart.labels = %w(total used free shared buffers cached)
5
+ chart.labels = [" total ", " used "]
8
6
 
9
7
  while true
10
8
  uptime = `free | grep Mem`.gsub(/^Mem:/, "").split(" ").map{|v| v.strip.to_f}
@@ -12,4 +10,7 @@ class Tplot::Free < Tplot::Plugin
12
10
  sleep 1
13
11
  end
14
12
  end
13
+
14
+ description "plot free commands result"
15
+ help "Usage: tplot free"
15
16
  end
data/plugins/help.rb ADDED
@@ -0,0 +1,24 @@
1
+ class Tplot::Help < Tplot::Plugin
2
+
3
+ description "print plugins help."
4
+
5
+ def execute
6
+ plugin_name = ARGV.shift
7
+ if File.exists?("#{PLUGIN_DIR}/#{plugin_name}.rb")
8
+ print_help("#{PLUGIN_DIR}/#{plugin_name}.rb", PLUGIN_DIR)
9
+ exit 0
10
+ end
11
+ if File.exists?("#{USER_PLUGIN_DIR}/#{plugin_name}.rb")
12
+ print_help("#{PLUGIN_DIR}/#{plugin_name}.rb", USER_PLUGIN_DIR)
13
+ exit 0
14
+ end
15
+ end
16
+
17
+ def print_help(plugin_path, dir)
18
+ require plugin_path
19
+ name = plugin_path.gsub("#{dir}/", '').gsub(".rb", '')
20
+ plugin_class = Tplot.const_get(name.capitalize)
21
+ return puts plugin_class.help.call if plugin_class.help.is_a? Proc
22
+ return puts plugin_class.help
23
+ end
24
+ end
data/plugins/list.rb CHANGED
@@ -3,12 +3,23 @@ class Tplot::List < Tplot::Plugin
3
3
  description "list installed plugins."
4
4
 
5
5
  def execute
6
- puts "-- installed plugins --"
7
- Dir.glob("#{BASE_DIR}/plugins/*.rb").each do |plugin|
8
- require plugin
9
- name = plugin.gsub("#{BASE_DIR}/plugins/", '').gsub(".rb", '')
10
- plugin_class = Tplot.const_get(name.capitalize)
11
- puts "tplot #{name}\t: #{plugin_class.description}"
6
+ # builtin plugin.
7
+ puts "-- builtin plugins --"
8
+ Dir.glob("#{PLUGIN_DIR}/*.rb").each do |plugin|
9
+ puts_description(plugin, PLUGIN_DIR)
12
10
  end
11
+
12
+ # user plugin.
13
+ puts "\n-- user plugins --" unless Dir.glob("#{USER_PLUGIN_DIR}/*.rb").empty?
14
+ Dir.glob("#{USER_PLUGIN_DIR}/*.rb").each do |plugin|
15
+ puts_description(plugin, USER_PLUGIN_DIR)
16
+ end
17
+ end
18
+
19
+ def puts_description(plugin, dir)
20
+ require plugin
21
+ name = plugin.gsub("#{dir}/", '').gsub(".rb", '')
22
+ plugin_class = Tplot.const_get(name.capitalize)
23
+ puts "tplot #{name}\t: #{plugin_class.description}"
13
24
  end
14
25
  end
@@ -0,0 +1,19 @@
1
+ class Tplot::Network < Tplot::Plugin
2
+
3
+ def execute
4
+ chart = Tplot::BarChart.new
5
+ chart.labels = %w(InOctets OutOctets)
6
+
7
+ while true
8
+ values = `cat /proc/net/netstat | tail -n 1 | cut -d" " -f8,9`.split(" ").map{|v| v.strip.to_f}
9
+ inoctet ||= values.first
10
+ outoctet ||= values.last
11
+ chart.add([values.first - inoctet, values.last - outoctet])
12
+ inoctet, outoctet = values.first, values.last
13
+ sleep 1
14
+ end
15
+ end
16
+
17
+ description "plot InOctets/OutOctets"
18
+ help "Usage: tplot network"
19
+ end
data/plugins/uptime.rb CHANGED
@@ -1,15 +1,38 @@
1
+ # -*- coding: utf-8 -*-
1
2
  class Tplot::Uptime < Tplot::Plugin
2
3
 
3
- description "plot uptime commands result"
4
+ def initialize
5
+ @options = {
6
+ chart_type: Tplot::BarChart,
7
+ delay: 1
8
+ }
9
+ end
10
+
11
+ def parser
12
+ OptionParser.new do |opt|
13
+ opt.banner = "Usage: tplot uptime [options]\n"
14
+ opt.on('-l', '--line','line chart.') do
15
+ @options[:chart_type] = Tplot::LineChart
16
+ end
17
+ opt.on('-n N', '--delay=N', 'set interval.') do |n|
18
+ @options[:delay] = n.to_i
19
+ end
20
+ end
21
+ end
4
22
 
5
23
  def execute
6
- chart = Tplot::BarChart.new
7
- chart.labels = %w(1min 5min 15min)
24
+ parser.parse!(ARGV)
25
+
26
+ chart = @options[:chart_type].new
27
+ chart.labels = [" 1min ", " 5min ", " 15min "]
8
28
 
9
29
  while true
10
30
  uptime = `uptime`.gsub(/^.*load average: /, "").split(",").map{|v| v.strip.to_f}
11
31
  chart.add(uptime)
12
- sleep 1
32
+ sleep @options[:delay]
13
33
  end
14
34
  end
35
+
36
+ description "plot uptime commands result"
37
+ help self.new.parser.to_s
15
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -31,9 +31,10 @@ files:
31
31
  - lib/tplot/line_chart.rb
32
32
  - lib/tplot/plugin.rb
33
33
  - lib/tplot/version.rb
34
- - log/debug.log
35
34
  - plugins/free.rb
35
+ - plugins/help.rb
36
36
  - plugins/list.rb
37
+ - plugins/network.rb
37
38
  - plugins/uptime.rb
38
39
  - tplot.gemspec
39
40
  homepage: https://github.com/mitoma/tplot