cloudstack-nagios 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +24 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +57 -0
  6. data/bin/cs-nagios +6 -0
  7. data/cloudstack-nagios.gemspec +28 -0
  8. data/lib/cloudstack-client/client.rb +136 -0
  9. data/lib/cloudstack-client/commands/account.rb +22 -0
  10. data/lib/cloudstack-client/commands/capacity.rb +19 -0
  11. data/lib/cloudstack-client/commands/cluster.rb +19 -0
  12. data/lib/cloudstack-client/commands/disk_offering.rb +49 -0
  13. data/lib/cloudstack-client/commands/domain.rb +22 -0
  14. data/lib/cloudstack-client/commands/host.rb +28 -0
  15. data/lib/cloudstack-client/commands/ip_address.rb +82 -0
  16. data/lib/cloudstack-client/commands/iso.rb +64 -0
  17. data/lib/cloudstack-client/commands/job.rb +29 -0
  18. data/lib/cloudstack-client/commands/load_balancer_rule.rb +61 -0
  19. data/lib/cloudstack-client/commands/network.rb +128 -0
  20. data/lib/cloudstack-client/commands/pod.rb +19 -0
  21. data/lib/cloudstack-client/commands/port_forwarding_rule.rb +49 -0
  22. data/lib/cloudstack-client/commands/project.rb +32 -0
  23. data/lib/cloudstack-client/commands/router.rb +89 -0
  24. data/lib/cloudstack-client/commands/server.rb +311 -0
  25. data/lib/cloudstack-client/commands/service_offering.rb +98 -0
  26. data/lib/cloudstack-client/commands/snapshot.rb +40 -0
  27. data/lib/cloudstack-client/commands/ssh_key_pair.rb +106 -0
  28. data/lib/cloudstack-client/commands/template.rb +60 -0
  29. data/lib/cloudstack-client/commands/user.rb +32 -0
  30. data/lib/cloudstack-client/commands/volume.rb +20 -0
  31. data/lib/cloudstack-client/commands/zone.rb +57 -0
  32. data/lib/cloudstack-client/version.rb +3 -0
  33. data/lib/cloudstack-nagios/base.rb +69 -0
  34. data/lib/cloudstack-nagios/cli.rb +100 -0
  35. data/lib/cloudstack-nagios/commands/nagios_config.rb +13 -0
  36. data/lib/cloudstack-nagios/helper.rb +17 -0
  37. data/lib/cloudstack-nagios/templates/cloudstack_routers_hosts.cfg.erb +36 -0
  38. data/lib/cloudstack-nagios/templates/cloudstack_routers_services.cfg.erb +35 -0
  39. data/lib/cloudstack-nagios/version.rb +3 -0
  40. data/lib/cloudstack_client.rb +2 -0
  41. data/lib/cloudstack_nagios.rb +4 -0
  42. metadata +155 -0
@@ -0,0 +1,82 @@
1
+ module CloudstackClient
2
+
3
+ module IpAddress
4
+
5
+ ##
6
+ # Lists the public ip addresses.
7
+
8
+ def list_public_ip_addresses(args = {})
9
+ params = {
10
+ 'command' => 'listPublicIpAddresses',
11
+ 'isrecursive' => true
12
+ }
13
+ if args[:project]
14
+ project = get_project(args[:project])
15
+ unless project
16
+ puts "Error: project #{args[:project]} not found."
17
+ exit 1
18
+ end
19
+ params['projectid'] = project['id']
20
+ end
21
+ if args[:account]
22
+ account = list_accounts({name: args[:account]}).first
23
+ unless account
24
+ puts "Error: Account #{args[:account]} not found."
25
+ exit 1
26
+ end
27
+ params['domainid'] = account["domainid"]
28
+ params['account'] = args[:account]
29
+ end
30
+ params['listall'] = args[:listall] if args[:listall]
31
+
32
+ json = send_request(params)
33
+ json['publicipaddress'] || []
34
+ end
35
+
36
+ ##
37
+ # Finds the public ip address for a given ip address string.
38
+
39
+ def get_public_ip_address(ip_address, project_id = nil)
40
+ params = {
41
+ 'command' => 'listPublicIpAddresses',
42
+ 'ipaddress' => ip_address
43
+ }
44
+ params['projectid'] = project_id if project_id
45
+ json = send_request(params)
46
+ ip_address = json['publicipaddress']
47
+
48
+ return nil unless ip_address
49
+ ip_address.first
50
+ end
51
+
52
+
53
+ ##
54
+ # Acquires and associates a public IP to an account.
55
+
56
+ def associate_ip_address(network_id)
57
+ params = {
58
+ 'command' => 'associateIpAddress',
59
+ 'networkid' => network_id
60
+ }
61
+
62
+ json = send_async_request(params)
63
+ json['ipaddress']
64
+ end
65
+
66
+ ##
67
+ # Disassociates an ip address from the account.
68
+ #
69
+ # Returns true if successful, false otherwise.
70
+
71
+ def disassociate_ip_address(id)
72
+ params = {
73
+ 'command' => 'disassociateIpAddress',
74
+ 'id' => id
75
+ }
76
+ json = send_async_request(params)
77
+ json['success']
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,64 @@
1
+ module CloudstackClient
2
+
3
+ module Iso
4
+
5
+ ##
6
+ # Lists all isos that match the specified filter.
7
+ #
8
+ # Allowable filter values are:
9
+ #
10
+ # * featured - isos that are featured and are public
11
+ # * self - isos that have been registered/created by the owner
12
+ # * self-executable - isos that have been registered/created by the owner that can be used to deploy a new VM
13
+ # * executable - all isos that can be used to deploy a new VM
14
+ # * community - isos that are public
15
+
16
+ def list_isos(args = {})
17
+ filter = args[:filter] || 'featured'
18
+ params = {
19
+ 'command' => 'listIsos',
20
+ 'isofilter' => filter
21
+ }
22
+ params['projectid'] = args[:project_id] if args[:project_id]
23
+ params['zoneid'] = args[:zone_id] if args[:zone_id]
24
+ if args[:listall]
25
+ params['listall'] = true
26
+ params['isrecursive'] = true
27
+ end
28
+
29
+ json = send_request(params)
30
+ json['iso'] || []
31
+ end
32
+
33
+ ##
34
+ # Finds the template with the specified name.
35
+
36
+ def get_iso(name)
37
+
38
+ # TODO: use name parameter
39
+ # listIsos in CloudStack 2.2 doesn't seem to work
40
+ # when the name parameter is specified. When this is fixed,
41
+ # the name parameter should be added to the request.
42
+ params = {
43
+ 'command' => 'listIsos',
44
+ 'isoFilter' => 'executable'
45
+ }
46
+ json = send_request(params)
47
+
48
+ isos = json['iso']
49
+ if !isos then
50
+ return nil
51
+ end
52
+
53
+ isos.each { |t|
54
+ if t['name'] == name then
55
+ return t
56
+ end
57
+ }
58
+
59
+ nil
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,29 @@
1
+ module CloudstackClient
2
+
3
+ module Job
4
+
5
+ ##
6
+ # Retrieves the current status of asynchronous job.
7
+
8
+ def query_job(id)
9
+ params = {
10
+ 'command' => 'queryAsyncJobResult',
11
+ 'jobid' => id,
12
+ }
13
+ send_request(params)
14
+ end
15
+
16
+ ##
17
+ # Lists all pending asynchronous jobs for the account.
18
+
19
+ def list_jobs(opts = {})
20
+ params = {
21
+ 'command' => 'listAsyncJobs'
22
+ }
23
+ params['listall'] = true if opts[:listall]
24
+ send_request(params)['asyncjobs']
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,61 @@
1
+ module CloudstackClient
2
+
3
+ module LoadBalancerRule
4
+
5
+ ##
6
+ # List loadbalancer rules
7
+
8
+ def list_load_balancer_rules(options = {})
9
+ params = {
10
+ 'command' => 'listLoadBalancerRules',
11
+ }
12
+ params['name'] = options[:name] if options[:name]
13
+
14
+ if options[:project_name]
15
+ project = get_project(options[:project_name])
16
+ params['projectid'] = project['id']
17
+ end
18
+
19
+ json = send_request(params)
20
+ json['loadbalancerrule'] || []
21
+ end
22
+
23
+ ##
24
+ # Creates a load balancing rule.
25
+
26
+ def create_load_balancer_rule(name, publicip, private_port, public_port, options = {})
27
+ params = {
28
+ 'command' => 'createLoadBalancerRule',
29
+ 'name' => name,
30
+ 'privateport' => private_port,
31
+ 'publicport' => public_port,
32
+ 'publicipid' => get_public_ip_address(publicip)['id']
33
+ }
34
+ params['algorithm'] = options[:algorithm] || 'roundrobin'
35
+ params['openfirewall'] = options[:openfirewall] || true
36
+
37
+ json = send_async_request(params)
38
+ json['LoadBalancerRule']
39
+ end
40
+
41
+ ##
42
+ # Assigns virtual machine or a list of virtual machines to a load balancer rule.
43
+
44
+ def assign_to_load_balancer_rule(name, vm_names)
45
+ id = list_load_balancer_rules(name).first['id']
46
+
47
+ vm_ids = vm_names.map do |vm|
48
+ get_server(vm)['id']
49
+ end
50
+
51
+ params = {
52
+ 'command' => 'assignToLoadBalancerRule',
53
+ 'id' => id,
54
+ 'virtualmachineids' => vm_ids.join(',')
55
+ }
56
+ json = send_async_request(params)
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,128 @@
1
+ module CloudstackClient
2
+
3
+ module Network
4
+
5
+ ##
6
+ # Finds the network with the specified name.
7
+
8
+ def networkid_to_name(id, project_id=nil)
9
+ params = {
10
+ 'command' => 'listNetworks',
11
+ 'id' => id
12
+ }
13
+ params['projectid'] = project_id if project_id
14
+ json = send_request(params)
15
+
16
+ json['network'] ? json['network'].first['name'] : nil
17
+ end
18
+
19
+ ##
20
+ # Finds the network with the specified name.
21
+
22
+ def get_network(name, project_id = nil)
23
+ params = {
24
+ 'command' => 'listNetworks',
25
+ 'listall' => true
26
+ }
27
+ params['projectid'] = project_id if project_id
28
+ json = send_request(params)
29
+
30
+ networks = json['network']
31
+ return nil unless networks
32
+
33
+ networks.each { |n|
34
+ if n['name'] == name then
35
+ return n
36
+ end
37
+ }
38
+ nil
39
+ end
40
+
41
+ ##
42
+ # Finds the default network.
43
+
44
+ def get_default_network(zone = nil)
45
+ params = {
46
+ 'command' => 'listNetworks',
47
+ 'isDefault' => true
48
+ }
49
+ if zone
50
+ params['zoneid'] = get_zone(zone)['id']
51
+ end
52
+ json = send_request(params)
53
+
54
+ networks = json['network']
55
+ return nil if !networks || networks.empty?
56
+ return networks.first if networks.length == 1
57
+
58
+ networks.each { |n|
59
+ if n['type'] == 'Direct' then
60
+ return n
61
+ end
62
+ }
63
+ nil
64
+ end
65
+
66
+ ##
67
+ # Lists all available networks.
68
+
69
+ def list_networks(args = {})
70
+ params = {
71
+ 'command' => 'listNetworks',
72
+ 'listall' => true,
73
+ }
74
+ params['projectid'] = args[:project_id] if args[:project_id]
75
+ params['zoneid'] = args[:zone_id] if args[:zone_id]
76
+ params['isDefault'] = true if args[:isdefault]
77
+ if args[:account]
78
+ domain = list_accounts(name: args[:account])
79
+ if domain.size > 0
80
+ params['account'] = args[:account]
81
+ params['domainid'] = domain.first["domainid"]
82
+ else
83
+ puts "Account #{args[:account]} not found."
84
+ end
85
+ end
86
+ json = send_request(params)
87
+ json['network'] || []
88
+ end
89
+
90
+ ##
91
+ # Delete network.
92
+
93
+ def delete_network(id)
94
+ params = {
95
+ 'command' => 'deleteNetwork',
96
+ 'id' => id,
97
+ }
98
+ p json = send_async_request(params)
99
+ json['network']
100
+ end
101
+
102
+ ##
103
+ # Restart network.
104
+
105
+ def restart_network(id, cleanup = false)
106
+ params = {
107
+ 'command' => 'restartNetwork',
108
+ 'id' => id,
109
+ 'cleanup' => cleanup,
110
+ }
111
+ p json = send_async_request(params)
112
+ json['network']
113
+ end
114
+
115
+ ##
116
+ # Lists all physical networks.
117
+
118
+ def list_physical_networks
119
+ params = {
120
+ 'command' => 'listPhysicalNetworks',
121
+ }
122
+ json = send_request(params)
123
+ json['physicalnetwork'] || []
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,19 @@
1
+ module CloudstackClient
2
+
3
+ module Pod
4
+
5
+ ##
6
+ # Lists pods.
7
+
8
+ def list_pods(args = {})
9
+ params = {
10
+ 'command' => 'listPods',
11
+ }
12
+
13
+ json = send_request(params)
14
+ json['pod'] || []
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,49 @@
1
+ module CloudstackClient
2
+
3
+ module PortForwardingRule
4
+
5
+ ##
6
+ # Lists all port forwarding rules.
7
+
8
+ def list_port_forwarding_rules(ip_address_id=nil, project_id)
9
+ params = {
10
+ 'command' => 'listPortForwardingRules',
11
+ 'listall' => true,
12
+ 'isrecursive' => true
13
+ }
14
+ params['ipAddressId'] = ip_address_id if ip_address_id
15
+ params['projectid'] = project_id if project_id
16
+ json = send_request(params)
17
+ json['portforwardingrule'] || []
18
+ end
19
+
20
+ ##
21
+ # Gets the SSH port forwarding rule for the specified server.
22
+
23
+ def get_ssh_port_forwarding_rule(server, cached_rules=nil)
24
+ rules = cached_rules || list_port_forwarding_rules || []
25
+ rules.find_all { |r|
26
+ r['virtualmachineid'] == server['id'] &&
27
+ r['privateport'] == '22'&&
28
+ r['publicport'] == '22'
29
+ }.first
30
+ end
31
+
32
+ ##
33
+ # Creates a port forwarding rule.
34
+
35
+ def create_port_forwarding_rule(ip_address_id, private_port, protocol, public_port, virtual_machine_id, async = true)
36
+ params = {
37
+ 'command' => 'createPortForwardingRule',
38
+ 'ipAddressId' => ip_address_id,
39
+ 'privatePort' => private_port,
40
+ 'protocol' => protocol,
41
+ 'publicPort' => public_port,
42
+ 'virtualMachineId' => virtual_machine_id
43
+ }
44
+ async ? send_async_request(params)['portforwardingrule'] : send_request(params)
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,32 @@
1
+ module CloudstackClient
2
+
3
+ module Project
4
+
5
+ ##
6
+ # Get project by name.
7
+
8
+ def get_project(name)
9
+ params = {
10
+ 'command' => 'listProjects',
11
+ 'name' => name,
12
+ 'listall' => true,
13
+ }
14
+ json = send_request(params)
15
+ json['project'] ? json['project'].first : nil
16
+ end
17
+
18
+ ##
19
+ # Lists projects.
20
+
21
+ def list_projects
22
+ params = {
23
+ 'command' => 'listProjects',
24
+ 'listall' => true,
25
+ }
26
+ json = send_request(params)
27
+ json['project'] || []
28
+ end
29
+
30
+ end
31
+
32
+ end