#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; use Getopt::Long; use threads; use LWP::UserAgent; my $version = '3.0'; my $build = '07302015'; my $debug = ""; my $maxThreads = '25'; my $countLimit = '0'; my $deleteLimit = '10000'; my $environment = "dev"; my $report_filenme = "deletedDevices"; my @row; my $deviceLogDIR; chomp (my $reportDate = `date +\%Y\%m\%d`); my @deletedDevices; my %active_threads = (); #Config Output Options my $output = CWM_configDelete->get_output(); my $date = "$output->{date}"; my $out_dir = "$output->{output_dir}"; chomp ($date); #Config Curl Options(Pulling these from a config.pm file I created as we have multiple environments) my $api_user = CWM_configDelete->get_api(); my $api_creds = "$api_user->{user}:$api_user->{pass}"; my $get_env = "get_$environment"; my $cwm_cfg = CWM_configDelete->$get_env(); my $cwm_url = $cwm_cfg->{url}; my $cwm_host = $cwm_cfg->{host}; my $cwm_user = $cwm_cfg->{user}; my $cwm_pass = $cwm_cfg->{pass}; my $cwm_db = $cwm_cfg->{DB}; #CMD Line Options GetOptions ("l:s" => \$deleteLimit, "e:s" => \$environment, "t:s" => \$maxThreads, 'debug' => \$debug); #####MAIN##### #Connect to the DB my $dbh = DBI->connect("DBI:mysql:database=$cwm_db;host=$cwm_host","$cwm_user","$cwm_pass", {'RaiseError' => 1}); #Prepare SELECT statement my $sth = $dbh->prepare("SELECT cmt.serialNumber FROM Device d JOIN CwmpManagedTrait cmt on (d.cwmpManagedTrait_id=cmt.id) WHERE cmt.lastinform < 1000*UNIX_TIMESTAMP(DATE_SUB(now(),INTERVAL 90 DAY)) limit $deleteLimit"); #Execute Select $sth->execute(); # THREADED ###Parse Fetch and execute DELETE while (@row = $sth->fetchrow_array ) { ###Create and Run threads $active_threads{@row} = threads->create (\&deleteDevice, @row); ###Limit running threads while(scalar threads->list(threads::running) >= $maxThreads) { foreach my $th_key(keys %active_threads) { next unless($active_threads{$th_key}->is_joinable()); $active_threads{$th_key}->join(); delete($active_threads{$th_key}); } sleep(1); } } ###Thread cleanup foreach (threads->list()) { $_->join(); } ### SUBS ### sub deleteDevice($) { my $deviceSN = shift; my $full_url = "https://$api_creds\@$cwm_url$deviceSN"; &debug("CMD: $full_url"); my $threadCount = scalar threads->list(threads::running); #print "Current Thread Count: $threadCount\r"; ## Get current subscriber info my $ua = LWP::UserAgent->new( #ssl_opts => { verify_hostname => 0, SSL_verify_mode => 0x00 } ); $ua->agent("Device Deletion"); my $req = HTTP::Request->new(DELETE => $full_url); $req->content_type('application/x-www-form-urlencoded'); my $res = $ua->request($req); if($res->is_success) { &logDeviceDetails("$deviceSN -- Delete Successful"); } else { &logDeviceDetails("$deviceSN -- Delete Failed: " . $res->status_line . " - " . $res->content); } } sub logDeviceDetails(@) { # Log Script actions my @deviceLogs = @_; chomp (my $TS = `date +\%H:\%M:\%S`); my $logoutFile = '/var/tmp/CWM/del_reports'; # Append File name $deviceLogDIR = "$logoutFile" . "/subDELreport" . '_' . "$reportDate"; # Create log file open FILE, ">>", "$deviceLogDIR" or die "Failed to open $deviceLogDIR: $!"; print FILE for "$TS: @deviceLogs\n"; #update file with edits made close FILE; } sub debug(@)#Spit Info { my @pass_data = @_; chomp (my $TS = `date +\%H:\%M:\%S`); if ($debug) { print for "DEBUG:$TS @pass_data\n"; } }