Skip to content

Commit

Permalink
feat: add used ip monitor
Browse files Browse the repository at this point in the history
Signed-off-by: zufardhiyaulhaq <[email protected]>
  • Loading branch information
zufardhiyaulhaq committed Jun 6, 2024
1 parent c116271 commit 0ff0034
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func (e *Collector) Collect(ch chan<- prometheus.Metric) {
for _, unusedIP := range unusedIPs {
ch <- prometheus.MustNewConstMetric(model.IPAddressUnusedGauge, prometheus.GaugeValue, 1, unusedIP.Cloud, unusedIP.Region, unusedIP.Value, unusedIP.Type, unusedIP.Identity)
}

usedIPs, err := e.google.GetUsedIP()
if err != nil {
return
}

for _, usedIP := range usedIPs {
ch <- prometheus.MustNewConstMetric(model.IPAddressUsedGauge, prometheus.GaugeValue, 1, usedIP.Cloud, usedIP.Region, usedIP.Value, usedIP.Type, usedIP.Identity)
}
}

if e.settings.EnableAWS {
Expand All @@ -40,6 +49,15 @@ func (e *Collector) Collect(ch chan<- prometheus.Metric) {
for _, unusedIP := range unusedIPs {
ch <- prometheus.MustNewConstMetric(model.IPAddressUnusedGauge, prometheus.GaugeValue, 1, unusedIP.Cloud, unusedIP.Region, unusedIP.Value, unusedIP.Type, unusedIP.Identity)
}

usedIPs, err := e.aws.GetUsedIP()
if err != nil {
return
}

for _, usedIP := range usedIPs {
ch <- prometheus.MustNewConstMetric(model.IPAddressUsedGauge, prometheus.GaugeValue, 1, usedIP.Cloud, usedIP.Region, usedIP.Value, usedIP.Type, usedIP.Identity)
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/data/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,57 @@ func (g *AWSData) GetUnusedIP() ([]model.IPAddress, error) {
return IPs, nil
}

func (g *AWSData) GetUsedIP() ([]model.IPAddress, error) {
var IPs []model.IPAddress

for _, assumeRoleRegion := range g.assumeRolesRegions {
assume := strings.Split(assumeRoleRegion, ",")
role := assume[0]
region := assume[1]

assumeRoleOutput, err := g.stsClient.AssumeRole(context.TODO(), &sts.AssumeRoleInput{
RoleArn: &role,
DurationSeconds: aws.Int32(int32(g.assumeRoleDuration)),
})
if err != nil {
return nil, err
}

credentialProvider := credentials.NewStaticCredentialsProvider(
*assumeRoleOutput.Credentials.AccessKeyId,
*assumeRoleOutput.Credentials.SecretAccessKey,
*assumeRoleOutput.Credentials.SessionToken,
)
cfg := aws.Config{
Credentials: credentialProvider,
Region: region,
}

ec2Client := ec2.NewFromConfig(cfg)

input := &ec2.DescribeAddressesInput{}
output, err := ec2Client.DescribeAddresses(context.TODO(), input)
if err != nil {
return nil, err
}

for _, address := range output.Addresses {
if address.AllocationId != nil && address.PublicIp != nil {
IPs = append(IPs, model.IPAddress{
Cloud: "AWS",
Region: region,
Value: *address.PublicIp,
Type: "Public",
Used: true,
Identity: "Elastic IP",
})
}
}

}
return IPs, nil
}

func NewAWSData(settings settings.Settings) (*AWSData, error) {
awsData := &AWSData{
assumeRolesRegions: settings.AWSAssumeRolesRegions,
Expand Down
1 change: 1 addition & 0 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import "github.com/gopaytech/unused-exporter/pkg/model"

type Data interface {
GetUnusedIP() ([]model.IPAddress, error)
GetUsedIP() ([]model.IPAddress, error)
}
28 changes: 28 additions & 0 deletions pkg/data/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ func (g *GoogleData) GetUnusedIP() ([]model.IPAddress, error) {
return IPs, nil
}

func (g *GoogleData) GetUsedIP() ([]model.IPAddress, error) {
var IPs []model.IPAddress

for _, project := range g.projects {
aggregatedList, err := g.computeService.Addresses.AggregatedList(project).Do()
if err != nil {
return nil, err
}

for region, addressesScopedList := range aggregatedList.Items {
for _, address := range addressesScopedList.Addresses {
if address.Status != "RESERVED" {
IPs = append(IPs, model.IPAddress{
Cloud: "GCP",
Region: region,
Identity: project + "/" + address.Name,
Value: address.Address,
Type: address.AddressType,
Used: true,
})
}
}
}
}

return IPs, nil
}

func NewGoogleData(settings settings.Settings) (*GoogleData, error) {
g := &GoogleData{
projects: settings.GCPProjects,
Expand Down
6 changes: 6 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ var (
"Indicates unused IP Address",
[]string{"cloud", "region", "value", "type", "identity"}, nil,
)

IPAddressUsedGauge = prometheus.NewDesc(
prometheus.BuildFQName("ip_address", "", "used"),
"Indicates used IP Address",
[]string{"cloud", "region", "value", "type", "identity"}, nil,
)
)

0 comments on commit 0ff0034

Please sign in to comment.