Skip to content

Instantly share code, notes, and snippets.

@synthead
Last active July 8, 2016 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synthead/53146db33b3b000ea0d802ae2fd2a44b to your computer and use it in GitHub Desktop.
Save synthead/53146db33b3b000ea0d802ae2fd2a44b to your computer and use it in GitHub Desktop.
<script>
var max_port_num = 8;
var port_middle_num = 16;
var all_info = {
state:[1,1,1,1,1,1,1,1,0,0],
link_status:[6,5,6,6,6,0,0,0,0,0],
pkts:[2122543,0,3700387,0,3680568,0,2159940,0,12396,0,5219,0,373395,0,315254,0,7230,0,595,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
};
var tip = "";
</script>
#!/usr/bin/ruby
require "rest-client"
require "yaml"
require "nokogiri"
require "rkelly"
require "zabbix"
def parse_global_js_vars(js)
rkelly_parser = RKelly::Parser.new
js_rkelly = rkelly_parser.parse(js)
js_rkelly.value.map do |variable|
[variable.value.first.name, parse_rkelly(variable.value.first.value.value)]
end.to_h
end
def parse_rkelly(node)
case node
when RKelly::Nodes::NumberNode, RKelly::Nodes::StringNode
node.value
when RKelly::Nodes::ArrayNode
node.value.map do |object|
parse_rkelly(object.value)
end
when RKelly::Nodes::ObjectLiteralNode
node.value.map do |object|
[object.name, parse_rkelly(object.value)]
end.to_h
end
end
@config = YAML.load(File.read("#{__FILE__}.yml"))
switch = RestClient::Resource.new("http://#{@config["switch"]["host"]}")
begin
switch["/logon.cgi"].post(
username: @config["switch"]["user"],
password: @config["switch"]["password"],
logon: "Login")
rescue RestClient::Unauthorized
end
port_stats = parse_global_js_vars(
Nokogiri::HTML(switch["/PortStatisticsRpm.htm"].get)
.xpath("//script").first.inner_html)
@zabbix = Zabbix::Sender.new(config_file: @config["zabbix"]["conf"])
def zabbix_send(port, key, value)
@zabbix.send_data(
"tp_link[#{port}][#{key}]", value, host: @config["zabbix"]["host"])
end
(1..port_stats["max_port_num"]).each do |port|
port_index = port - 1
all_info = port_stats["all_info"]
pkts_start = port_index * 4
zabbix_send(port, "status", all_info["state"][port_index])
zabbix_send(port, "link_status", all_info["link_status"][port_index])
[:tx_good_pkt, :tx_bad_pkt, :rx_good_pkt, :rx_bad_pkt]
.each_with_index do |field, index|
zabbix_send(port, field, all_info["pkts"][pkts_start + index])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment